Jump to content

Recommended Posts

Hi,

I'm wondering if anyone can point me to a site that explains clearly when to use the name attribute, and when to use id?  To now, I've been using them as duplicates of the same feature: <input name="foo" id="foo" ... />.

If my only use of these attributes is to access elements through document.getElementById(), can I just use id?  When do I need to use name?

Thanks

pnj
Link to comment
https://forums.phpfreaks.com/topic/26297-name-vs-id-reference/
Share on other sites

Thanks for the w3 site.

What I understand is that the name attribute is required to pass form information in an http get or post.  The id is necessary to access via getElementById().  So I guess it is correct to use name=x id=x for all form entries where I need both these options.

Yeah?

-pnj
Link to comment
https://forums.phpfreaks.com/topic/26297-name-vs-id-reference/#findComment-120294
Share on other sites

[quote author=pnj link=topic=113978.msg463592#msg463592 date=1162818579]
What I understand is that the name attribute is required to pass form information in an http get or post.  The id is necessary to access via getElementById().  So I guess it is correct to use name=x id=x for all form entries where I need both these options.
[/quote]

Let me ask this: why is it necessary to use [b]both[/b] name [b]and[/b] id fields for one element? You can reference an element by either, and since you [i]typically[/i] aren't going to be using the same javascript functions one input fields as you would on other elements throughout your document, why not just reference by name? I can't say that there is a right and wrong way to use them, but there is definitely a point when you want to optimize your code and leave your markup as clean as possible while allowing your javascript to be flexible.

Can you give us an example of why you would need both?
Link to comment
https://forums.phpfreaks.com/topic/26297-name-vs-id-reference/#findComment-120296
Share on other sites

I think I do need both.  The input fields are in a form that is being submitted in a POST, so I need the names.

The validation function checks the elements by walking through an array of id names and loading form elements with getElementById().  The reason I don't just walk through document.forms[] is because the validation function is generated in php, and the array with id names also has a lot of information on required fields, min/max values, field types and so on.  Perhaps this function can be improved...  right now it looks something like this in pseudocode:

[code]ids = Array ( ... list of ids generated from php ... )
types = Array ( ... list of types (i.e. text, integer, decimal, date) generated from php ... )
reqs = Array ( ... 0 or 1 says whether each element is defined ... )

for (i=0; i < ids.length; i++) {
  // validate ids[i], based on info in types[i] and reqs[i];
}
[/code]

While I could put the three arrays into an array of objects with the same characteristics, this seems pretty much equivalent to me.  Do you see a better way to structure this?

Thanks

pnj
Link to comment
https://forums.phpfreaks.com/topic/26297-name-vs-id-reference/#findComment-120302
Share on other sites

Once you have a form's id, you don't need an id for each input.  So long as the inputs have a name (which, in your case, they must), you can still access them.

[code]
var form = document.getElementById('formId');
var name = form.name;
var email = form.email;
var password = form.password;
.
.
.
//this example assumes your form has inputs with the names name, email, and password
[/code]
Link to comment
https://forums.phpfreaks.com/topic/26297-name-vs-id-reference/#findComment-120348
Share on other sites

Alright, this gets me halfway there.

But what do you suggest for store additional metadata about form fields that I can access in a validation function?

Ideally, I would like to do something like this:
[code]
<input name='first_name' type='text' format='text' required='1'>
<input name='height' type='text' format='integer' required='0' min='15' max='200'>
[/code]

And then be able to access those additional attributes in a validation function through form.elements.  But as far I know, the attributes in the code above will be thrown away by the DOM.

How should I think about approaching this?

Thanks!
-pnj
Link to comment
https://forums.phpfreaks.com/topic/26297-name-vs-id-reference/#findComment-120817
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.