Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Option element with data attributes. Is it posible and if so, how to retrive value with jQuery?

Tags:

html

jquery

I just wonder if is it posible to have option elements in html with data- attributes and so, if that posible how to retrive this value.

More specific. Lets say I have the following code:

<select name="areas" id="areas">
    <option value="1" data-lat="12.243" data-lng="32.43242">Area name</option>
    <option value="2" data-lat="14.243" data-lng="34.43242">Area name</option>
    <option value="3" data-lat="16.243" data-lng="36.43242">Area name</option>
    <option value="4" data-lat="18.243" data-lng="38.43242">Area name</option>
</select>

then, is it any way to get the data-lat and data-lng for from the selected option via jQuery ?

like image 544
KodeFor.Me Avatar asked Oct 04 '13 17:10

KodeFor.Me


People also ask

How do you get the value of a data attribute?

To get a data attribute through the dataset object, get the property by the part of the attribute name after data- (note that dashes are converted to camelCase). Each property is a string and can be read and written.

Can option tag have data attribute?

An <option> element can have any number of data-* attributes, each with their own name. Using data-* attributes reduces the need for requests to the server. Note: The data-* attribute is not visible and does not change the appearance of the option.

How do I select HTML element based on data attribute?

Use the querySelector method to get an element by data attribute, e.g. document. querySelector('[data-id="box1"]') . The querySelector method returns the first element that matches the provided selector or null if no element matches the selector in the document. Here is the HTML for the examples in this article.

How get data attribute value in jQuery?

Answer: Use the jQuery attr() Method You can simply use the jQuery attr() method to find the data-id attribute of an HTML element.


4 Answers

Yes that's possible. Any attribute value can be get by:

$('option:selected').attr('data-lng')

and set by:

$('option:selected').attr('data-lng', 1234);
like image 117
Bernhard Avatar answered Oct 07 '22 07:10

Bernhard


Set attribute using setAttribute:

document.getElementById('areas')
    .getElementsByTagName('option')[index]
    .setAttribute('data-lng', 'foo');

Get attribute using getAttribute:

document.getElementById('areas')
    .getElementsByTagName('option')[index]
    .getAttribute('data-lng');

Both get and set:

var el = document.getElementById('areas')
    .getElementsByTagName('option')[index];
el.getAttribute('data-lng');
el.setAttribute('data-lng', 'foo');

Note 1 I have used document.getElementById('areas').getElementsByTagName('option')[index] because probably is more cross-browser, but you could use

  • document.getElementById('areas').getElementsByTagName('option')[index]
  • document.getElementById('areas').options[index]
  • document.getElementById('areas')[index]

Note 2 I have used setAttribute and getAttribute because they are more cross-browser, but you could also use dataset:

el.dataset.lng;            // get
el.dataset.lng = 'foo';    // set
like image 29
Oriol Avatar answered Oct 07 '22 07:10

Oriol


You can use .data to retrieve data attributes from elements

$('#areas options').each(function(){
    console.log($(this).data('lat'), $(this).data('lng'));
}

The advantage of this over .attr is this is that you get type conversion so you'll get a Numeric value in this case rather than a string.

like image 24
Musa Avatar answered Oct 07 '22 05:10

Musa


I'd like to add, If you want to target a specific <select>

You can also pass the container element so that <options> from only that element will be selected.

// For user with events like .on("change") etc.
console.log($("option:selected", this).attr("data-lat")); 

OR

console.log($("option:selected", "#areas").attr("data-lat"));
like image 39
Mohd Abdul Mujib Avatar answered Oct 07 '22 07:10

Mohd Abdul Mujib