Get jquery data attributes from a form's select's option's values
I'm trying to get the values of data from the options in the form below,
so I can use them in jQuery, but I somehow don't seem to be succeeding.
What I'm trying to do is simple, and you'll see in my code below. How can
I do something like this?
$('#selectForm').click(function(e) {
e.preventDefault();
var action = $(this).data('id');
var type = $(this).data('name');
console.log(action+' '+type);
});
//Jquery
<form id="selecttest">
<label for="fruit">Please select at least two fruits</label><br>
<select id="fruit" name="fruit" size="5">
<option data-id="1" data-name="norman">Banana</option>
<option data-id="2" data-name="james">Apple</option>
<option data-id="3" data-name="lars">Peach</option>
<option data-id="4" data-name="john">Turtle</option>
</select>
<input type="submit" id="selectForm" value="Validate Selecttests">
</form>
No comments:
Post a Comment