Javascript - Get Atribute From Select Menu September 16, 2024 Post a Comment Hello there dear community. Here is my HTML code: Избран цвят: Blue/Lt Blue Solution 1: I would do this way:$('#image option[value="Blue/Lt Blue"]').attr("color-number"); CopySo essentially you search for the correct option and get it attribute.Solution 2: Using :contains() won't be a proper solution as it will return partial matches, instead you can use .filter()var text = 'Black/Pink'; var cn = $('select[name="Pcolor"] option').filter(function() { return $(this).text().trim() == text; }).attr('color-number'); console.log(cn)Copy<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><selectname="Pcolor"id="image"style="height:30px;"><optionvalue="">Избран цвят: Blue/Lt Blue</option><optionvalue="45751303"color-number="0">Black</option><optionvalue="45751343"color-number="1">Black/Pink</option><optionvalue="45751359"color-number="2">Blue/Lt Blue</option><optionvalue="45751324"color-number="3">Dk Purple/Purpl</option><optionvalue="45751390"color-number="4">Ink/Cerise</option></select>CopySolution 3: $('#image').on('change',function() { alert($(this).find('option:selected').attr('color-number')); }) Copyhttps://jsfiddle.net/cw6knsc5/2/ Share Post a Comment for "Javascript - Get Atribute From Select Menu"
Post a Comment for "Javascript - Get Atribute From Select Menu"