logiacer logiacer - 2 months ago 5
Javascript Question

Get name from option HTML with Javascript

<option value="1" name="13.890000">Monster</option>


I would like to get name via javascript to do calculations (On change) (NOTE: NOT VALUE)
Suggestions?

Answer

Using, $(this).children(':selected').attr('name'); Your code should be something like: Here is the snippet

$("#select").on('change', function() {
  x =  $(this).children(':selected').attr('name');
  alert('the name is: '+x);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="select">
<option id ="#option1" value="1" name="13.890000">Monster</option>
<option id ="#option2" value="2" name="13.890000">Monster</option>

</select>

Comments