In the following div how can i get the value=12 value when the div is clicked
<div value='12' id='div1'>some content</div>
$('#div1').live("click", function(a) {
alert(this.value);
alert($('#div1').val()); //doesnt work
});
use jquery attr()
Get the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element.
try this
alert($('#div1').attr('value'));
OR
$('#div1').on("click", function(a) {
alert($(this).attr('value')); //doesnt work
});
If you can keep away from using your own HTML attributes you'll run into validation issues, a better choice would html5's data attributes. This allows you to create your own attributes good for holding this type of data. For instance:
<div data-num="12" id="div1">some content</div>
$('#div1').on('click', function(){
alert($(this).data('num'));
});
http://api.jquery.com/jQuery.data/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With