javascript - jquery multiple selector with contains() -


i've lot of <option>s in <select>and , attribute called zip

<select id="city">   <option zip="auc 1291 100005" value="1">city 1</option>   <option zip=" 1295 100006" value="2">city 2</option>   <option zip=" 1299 100008" value="3">city 3</option> </select> 

now want select option text or zip contains string value, following code doens't work.

var val = '1291'; //zip code var x = $('#city option:contains('+val+'), #city zip:contains('+val+')').val(); 

why?

if want select elements attribute, taht value contains requested string, have use:

  var val = '1291'; //zip code   var x = $('#city option[zip*="' + val + '"]').val(); 

as mentioned: http://api.jquery.com/attribute-contains-selector/

but guys said, better way have html5 data-zip attribute instead of zip

(just prefix data-).

in case, selector be:

  var x = $('#city option[data-zip*="' + val + '"]').val(); 

hope helped.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -