JQuery click image, go to option selection

Hi guys, I have 3 images and a choice with 3 options.

The parameter value matches the alt image. How to do this, when I click on the image, it takes an alt and chooses the correct option?

Hope you got it.

Thanks.

+4
source share
3 answers

Take a look at the violin and let me know if you have any questions. the code is a little tight

http://jsfiddle.net/phfS8/1/

<select> <option value="google">google</option> <option value="yahoo">yahoo</option> </select> <img alt="google" src="http://www.google.com/favicon.ico"/> <img alt="yahoo" src="http://www.yahoo.com/favicon.ico"/> $('select').val(); $('img').click(function(){ $('option[value="' + $(this).attr("alt") + '"]').attr('selected', true); }); 
+2
source
 $('img').click(function () { $('select').val(this.alt); }); 

Replace img and select with more specific selectors if necessary.

+6
source

Box has a nice solution, but it's about 25-33% faster in firefox. You can check the performance log for both selectors http://jsperf.com/img-select00

 $('img').click(function() { $('select option').eq(this.alt - 1).attr('selected', 'true'); }) 

Check out the working example at http://jsfiddle.net/zkebs/1/

+2
source

Source: https://habr.com/ru/post/1347475/


All Articles