Javascript - Get Atribute From Select Menu March 26, 2023 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"); Copy So 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 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <select name="Pcolor" id="image" style="height:30px;"> <option value="">Избран цвят: Blue/Lt Blue</option> <option value="45751303" color-number="0">Black</option> <option value="45751343" color-number="1">Black/Pink</option> <option value="45751359" color-number="2">Blue/Lt Blue</option> <option value="45751324" color-number="3">Dk Purple/Purpl</option> <option value="45751390" color-number="4">Ink/Cerise</option> </select>Copy Solution 3: $('#image').on('change',function() { alert($(this).find('option:selected').attr('color-number')); }) Copy https://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"