Solved

Form elements names and ids with jquery

Posted on 2014-12-02
3
63 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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…

830 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