Avatar of andieje
andieje
 asked on

Form elements names and ids with jquery

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
jQuery

Avatar of undefined
Last Comment
andieje

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Gary

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
leakim971

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

andieje

ASKER
thanks expert :)
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck