I have a simple list like this:
<ul id="large_box_custom_list">
<li id="8" class="large_box">Item 1</li>
<li id="13" class="large_box">Item 2</li>
</ul>
then I have a jQuery function like this:
$(function() {
$('li.large_box').css('cursor', 'pointer')
.click(function() {
var show_id = $(this).val();
alert(show_id);
});
});
When I click the list items my alery shows a value of 0 when I am expecting a value of 8 or 13.
jQuery val() Method The val() method returns or sets the value attribute of the selected elements. When used to return value: This method returns the value of the value attribute of the FIRST matched element.
jQuery val() method is used to get the value of an element. This function is used to set or return the value. Return value gives the value attribute of the first element. In case of the set value, it sets the value of the attribute for all elements.
val() method is primarily used to get the values of form elements such as input , select and textarea . When called on an empty collection, it returns undefined . When the first element in the collection is a select-multiple (i.e., a select element with the multiple attribute set), .
Because you should be using the standard DOM element id
property. jQuery's .val()
method has nothing to do with an element's ID.
$('li.large_box').css('cursor', 'pointer').click(function ()
{
var show_id = this.id;
alert(show_id);
});
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