jQuery Auto-Selecting an option in drop-down -


i trying auto-select option in drop down when specific image clicked, have made jsfiddle show current code, can point me in right direction?

link: http://jsfiddle.net/cwtx6/

html:

<img class="auto-image" data-name="01" style="cursor:pointer;" src="img/plaque/horses/01.png">  <select name="productoption[]" id="product-option-11" class="product-option product-option-select" data-force="0" data-title="image">     <option value="0" data-price="0">please select...</option>     <option value="11::167" data-price="0.0000" data-modify="0">01</option> </select> 

js:

$('.auto-image').on('click', function() {     var search = $(this).data('name');     console.log('click being run, search has been varred as: ' + search);      $('.product-option-select').find('option[text=' + search + ']').attr('selected', 'selected');      var found = $('.product-option-select').find('option[text=' + search + ']').val();     console.log('oh, , found option, value is: ' + found); }); 

at moment console displays this:

click being run, search has been varred as: 01 oh, , found option, value is: undefined  

cheers

use demo fiddle

$('.product-option-select').find('option:contains(' + search + ')') 

  1. find('option[text=' + search + ']') wrong selector in case. use :contains()
  2. you did not include reference jquery in fiddle.

full js code- modified :

$('.auto-image').on('click', function() {     var search = $(this).data('name');     console.log('click being run, search has been varred as: ' + search);      $('.product-option-select').find('option:contains(' + search + ')').attr('selected', 'selected');      var found = $('.product-option-select').find('option:contains(' + search + ')').val();      console.log('oh, , found option, value is: ' + found); }); 

alternative :contains(),

$(".product-option-select option").filter(function () {     return $.trim($(this).text()) == search; }).attr('selected', 'selected'); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -