Sunil
Sunil

Reputation: 21396

Multiple condition selector in jQuery

I have the following code, and I want to select the element with type = text and id of 'announcement_date'. How would I do this in jQuery? Note that both the elements have same id. This is in a legacy classic ASP page I am working with.

<input type="hidden" name="announcement_date" id="announcement_date" value="1111"></input>
<input type="text" name="announcement_date" id="announcement_date" value="323"></input>
<input type="text" name="eff_date" id="eff_date" value="123"></input>
<input type="text" name="end_date" id="end_date" value="123"></input>

I tried the selector below, but it fails to select the correct element.

$("#announcement_date type=text").addClass("error");

Upvotes: 4

Views: 11137

Answers (4)

Isaac Gonzalez
Isaac Gonzalez

Reputation: 1734

<input type="text" name="announcement_date"
class="announcement_date" value="1111" />

<input type="submit" name="announcement_date"
class="announcement_date" value="Submit" />

and then ​

$(':text.announcement_date').addClass("error");

Demo

Upvotes: 0

keithxm23
keithxm23

Reputation: 1280

I believe you could use this idea.

So you could use..

$('input[type="text"][name="announcement_date"]')

Here is a related SO question which is similar.

Upvotes: 0

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195982

IDs have to be unique.

(if for some reason you cannot change the html) you can select by name though as

$('input[name="announcement_date"][type="text"]');

Upvotes: 11

Hogan
Hogan

Reputation: 70513

You should just use id since having the same id on two elements is illegal in HTML.

(That is, fix your html to have unique ids then use id to select.)

Upvotes: 1

Related Questions