37

I have searched but cannot find the jQuery selector that gets an element by a title. So I would be trying to get a handle on the dropdown with title =='cars'

Eg:

<select title='cars'>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

Thanks alot,

Doug Neiner
  • 65,509
  • 13
  • 109
  • 118
van
  • 9,159
  • 19
  • 60
  • 93

2 Answers2

98

Use the attribute-equals selector, like this:

$("select[title='cars']")

There are other attribute selectors available as well if the need arises :)

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 1
    Thanks Nick, sorry took so long to mark as answer, i thought it wasn't working in I.E 6 but thats because i had a console.log message throwing a javascript error! Many Thanks – van Jun 20 '10 at 18:21
  • One may need to check a followup question I had here...http://stackoverflow.com/questions/19851247/linking-html-element-to-jquery-click-using-attribute-title – user1517108 Nov 08 '13 at 13:50
  • Note that the select in this code snippet is for the HTML tag select. $("[title|='cars']") gets all the elements with the title cars with any HTML tag. – Pat Myron Oct 22 '18 at 22:57
-1

While Nick's answer solves the problem for many, it didn't for me due to a simple reason of it being case-sensitive.

Appending the attribute value with i would make it case-insensitive. Try this:

$("select[title='cars' i]")

This would also work in case your title is dynamic and might come with a capital case as Cars rather than static cars.

Reference for case insensitivity flag i: https://stackoverflow.com/a/38923109/4365626

Vibhor Dube
  • 4,173
  • 1
  • 22
  • 32