Add select option with HTML5 data attribute using jQuery

I am trying to build a parameter set for a select element using jQuery. It works, with the exception of the data attribute, which should be included, which seems to be omitted.

Here is my code:

// cJ is JSON object. // #currency is my select element. function makeCurDropDown(cJ) { $.each(cJ, function (i) { $('#currency').append( $('<option></option>').val(i).data("sym", cJ[i][0]).html(cJ[i][1]) ); }); } 
+4
source share
1 answer

Actually, the data parameter is set, but since you use the data method instead of the attr method, it is not displayed. jQuery stores the value behind the scenes to get a value that you can use .data('sym') . If you want to set the value as an attribute, you can use the attr method.

+6
source

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


All Articles