Jump to content

Problem Passing Variable In Function


ProblemHelpPlease
 Share

Recommended Posts

I am trying to write a function that alerts a user based on their input. The idea was to have several forms on 1 page with different id or name fields for each form but the same id or name for each input type within each form. I am trying to pass the number of tthe form to the function so it slelects the correct form but can't get the form to select properly

<form name='modifydailyentry1' id='modifydailyentry1' method='post' action='modify.php' onsubmit='return exactdates(1)'><select id='setyear' name='setyear'><option value='0'>-</option><option value='2008'>2008</option><option value='2009'>2009</option></select> </form>

Several copies of the above form are on the same page but the number after modifydailyentry increase by 1 for each form. The setyear name and id allways say setyear for each form

function exactdates(passme){var currentdateyIndex = document.modifydailyentry+passme.setyear.selectedIndex;var currentdatey = document.modifydailyentry+passme.setyear[currentdateyIndex].value;//other code runs here

Any ideas?

Link to comment
Share on other sites

Number uno, forms don't support the name or id attributes. Secondly, you can only use an id once- it is unique and cannot be used again. Sorry.

Link to comment
Share on other sites

I agree with the id tag only being used once, but I don't understand why you think forms cant have a name tag, or am I missing something?The following code works fine but I am unable to pass the variable and have to have multiple function of the same thing.

<form name='modifydailyentry1' method='post' action='modify.php' onsubmit='return exactdates()'><select name='setyear'><option value='0'>-</option><option value='2008'>2008</option><option value='2009'>2009</option></select> </form>

function exactdates(){var currentdateyIndex = document.modifydailyentry1.setyear.selectedIndex;var currentdatey = document.modifydailyentry1.setyear[currentdateyIndex].value;//other code runs here}

Link to comment
Share on other sites

I am sure I read that the name and id attributes of forms are deprecated. And I misread the thing about the forms having progressive ids. *puts on dunce hat*

Link to comment
Share on other sites

I'm basically trying to get a number from a variable as part of the name of the form in the script, but the quotes cause it to write var x rather than run it, and without the quotes is fails.

var q = 1;var x = "document.myForm + q + .asd.value";document.write(x);

Link to comment
Share on other sites

Of course a form element can have an id. All page elements can have an id.The name attribute is deprecated (an in some browsers won't even work) except for form elements (not the form itself), iFrames, and 1-2 others.This means that dot notation doesn't work in most browsers to get references to most elements, including a form. If names and dot notation is working, I'll bet you're using IE and haven't tested it extensively in other browsers.Dot notation DOES work when you already have a reference to a form and want references to the form elements. It's a subtle distinction, but important.The best solution in this instance is to use the this keyword. It simplifies a lot of things.(We also have to use the selectedIndex property correctly. A select element is neither an array or a collection. Its options property is, however.)

<form name='modifydailyentry1' method='post' action='modify.php' onsubmit='return exactdates(this)'>	<select name='setyear'>		<option value='0'>-</option>		<option value='2008'>2008</option>		<option value='2009'>2009</option>	</select> </form>

In the code above, code in the form tag passes a reference to this. Because it is in the scope of the form tag, this refers to the form itself. With that reference, we can use dot notation to get a reference to the select element.

function exactdates(myform){	var currentdateyIndex = myform.setyear.selectedIndex;	var currentdatey = myform.setyear.options[currentdateyIndex].value;	//other code runs here}

This code will work for every form that has a select element named "setyear".

Link to comment
Share on other sites

Like I said, names are fine for form elements, iFrames, and image maps. But they're so deprecated for other items that they aren't even mentioned as being deprecated. Check out, somewhat randomly, the page about the <div> element: http://www.w3schools.com/tags/tag_div.aspThe name attribute isn't even mentioned.A quick test verifies that Firefox will still recognize the name attribute of a <form> tag using dot notation, and in fact the W3C validator doesn't object to the name attribute in a <form> tag.So maybe with forms it'll stick around. But I wouldn't try it in any other context.

Link to comment
Share on other sites

Names are deprecated for forms in XHTML 1.0, but not in HTML 4.01 (look at the DTDs).
Errr... I think you mean XHML 1.1? 1.0 is only a reformulation of HTML 4.01 without any actual differences. I've used forms with XHTML 1.0 as text/html (with name attributes), and they validate just fine.
Link to comment
Share on other sites

Errr... I think you mean XHML 1.1? 1.0 is only a reformulation of HTML 4.01 without any actual differences. I've used forms with XHTML 1.0 as text/html (with name attributes), and they validate just fine.
Maybe I'm reading the DTDs incorrectly:HTML 4.01
<!ATTLIST FORM  %attrs;							  -- %coreattrs, %i18n, %events --  action	  %URI;		  #REQUIRED -- server-side form handler --  method	  (GET|POST)	 GET	   -- HTTP method used to submit the form--  enctype	 %ContentType;  "application/x-www-form-urlencoded"  accept	  %ContentTypes; #IMPLIED  -- list of MIME types for file upload --  name		CDATA		  #IMPLIED  -- name of form for scripting --  onsubmit	%Script;	   #IMPLIED  -- the form was submitted --  onreset	 %Script;	   #IMPLIED  -- the form was reset --  accept-charset %Charsets;  #IMPLIED  -- list of supported charsets --  >

XHTML 1.0
<!ENTITY % attrs "%coreattrs; %i18n; %events;">

<!ENTITY % coreattrs "id		  ID			 #IMPLIED  -- document-wide unique id --  class	   CDATA		  #IMPLIED  -- space-separated list of classes --  style	   %StyleSheet;   #IMPLIED  -- associated style info --  title	   %Text;		 #IMPLIED  -- advisory title --"  ><!ENTITY % i18n "lang		%LanguageCode; #IMPLIED  -- language code --  dir		 (ltr|rtl)	  #IMPLIED  -- direction for weak/neutral text --"  ><!ENTITY % events "onclick	 %Script;	   #IMPLIED  -- a pointer button was clicked --  ondblclick  %Script;	   #IMPLIED  -- a pointer button was double clicked--  onmousedown %Script;	   #IMPLIED  -- a pointer button was pressed down --  onmouseup   %Script;	   #IMPLIED  -- a pointer button was released --  onmouseover %Script;	   #IMPLIED  -- a pointer was moved onto --  onmousemove %Script;	   #IMPLIED  -- a pointer was moved within --  onmouseout  %Script;	   #IMPLIED  -- a pointer was moved away --  onkeypress  %Script;	   #IMPLIED  -- a key was pressed and released --  onkeydown   %Script;	   #IMPLIED  -- a key was pressed down --  onkeyup	 %Script;	   #IMPLIED  -- a key was released --"  >

<!ATTLIST form  %attrs;  action	  %URI;		  #REQUIRED  method	  (get|post)	 "get"  enctype	 %ContentType;  "application/x-www-form-urlencoded"  onsubmit	%Script;	   #IMPLIED  onreset	 %Script;	   #IMPLIED  accept	  %ContentTypes; #IMPLIED  accept-charset %Charsets;  #IMPLIED  >

Edited by Synook
Link to comment
Share on other sites

I think my position has more to do with logical consistency. Once you have a reference to a form element, using names and dot notation seems convenient and reasonable.But a form element is a container for this apparatus, not a part of the apparatus, and for this reason a name attribute is no more appropriate for a <form> than it would be for a <div> or a <p>.Besides which, the more exceptions there are to this rule, the more difficult it becomes to teach MSIE fans not to apply it everywhere. You've seen how much copied and pasted code comes through here using ancient constructions like document.all, and because MS still supports it, novice developers complain when Firefox doesn't.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...