Solved

Form elements names and ids with jquery

Posted on 2014-12-02
3
65 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

717 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