Malcolm
Malcolm

Reputation: 12874

Selecting input with JQuery selector

if i have the following:

<input type="text" id="something_txtYear" />

How do select this in JQuery using just the "txtYear" part??

Malcolm

Upvotes: 1

Views: 353

Answers (5)

Nick Johnson
Nick Johnson

Reputation: 914

Very important. You must have quotes around the value for jQuery 1.4.4 and above. This is how the selector should look now:

This does NOT work in jQuery 1.4.4

$('[id=txtYear]')

This does work for jQuery 1.4.4 and above:

$('[id="txtYear"]')

We had to update all of our code when we upgraded. This link gives a find and replace regular expression on how to fix this. http://nickjohnson.com/b/jquery-upgrade-how-to-fix-attribute-value-selector-errors

Upvotes: 0

A. Murka
A. Murka

Reputation: 212

Either $('[id$=txtYear]') to match only at the end of the id or $('[id*=txtYear]') to match anywhere in the id.

You'll want to look at: http://docs.jquery.com/Selectors , scroll down to the section on Attribute Filters.

Upvotes: 6

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827992

You can use the attribute endsWith selector

$('input[id$=txtYear]')

Upvotes: 1

meder omuraliev
meder omuraliev

Reputation: 186742

$('input[id*=txtYear]')

Upvotes: 1

eyelidlessness
eyelidlessness

Reputation: 63529

$('[id$=txtYear]')

Upvotes: 1

Related Questions