Solved

Form elements names and ids with jquery

Posted on 2014-12-02
3
59 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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks expert :)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to dynamically set the form action using jQuery.
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…

744 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

17 Experts available now in Live!

Get 1:1 Help Now