Solved

Form elements names and ids with jquery

Posted on 2014-12-02
3
61 Views
Last Modified: 2014-12-07
Hi

Can form elements have the same name and id such as
<input name="somename" id="somename">
and still work with jquery

I know each element must have a unique id but i was wondering if there was any reason why they couldnt have the same name and id

In fact I'm not even sure if they need both a name and an id. I know an id is convenient for referencing the element in code and i know elements needs a name to be serialised for form submission.

Thanks
0
Comment
Question by:andieje
3 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40476693
Yep, no problem
Do it quite often and sometimes makes for easier referencing.
You can still reference an element by it's name and forget the ID but the code is easier to write if there is an id

$("#myelement")
vs
$("[name=myelement]")
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40477002
that make lot of sense to have the same name for radio button and lso for checkbox

<input type="radio" name="answer1" value="1" />Yes<br />
<input type="radio" name="answer1" value="0" />No<br />

Open in new window


<input type="radio" name="options" value="Coffee" />Coffee<br />
<input type="radio" name="options" value="Milk" />Milk<br />
<input type="radio" name="options" value="Sugar" />Sugar<br />

Open in new window


you can additionaly add more than one selector, for example :

$(":radio[name=answer1][value=1]").attr("selected", "selected");

Open in new window

0
 

Author Closing Comment

by:andieje
ID: 40485592
thanks expert :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Open an ul or div with hover on a li,how? 9 36
update field on focusout 15 43
Bootstap Icons 3 43
Google maps isn't showing even after API has been entered 12 78
I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now