To retrieve a data-* attribute value as an unconverted string, use the attr() method. Since jQuery 1.6, dashes in data-* attribute names have been processed in alignment with the HTML dataset API. $( "div" ).
The jQuery. hasData() method provides a way to determine if an element currently has any values that were set using jQuery. data() . If there is no data object associated with an element, the method returns false ; otherwise it returns true .
Approach: First, select the element which is having data attributes. We can either use the dataset property to get access to the data attributes or use the . getAttribute() method to select them by specifically typing their names.
Inside my view i have a button as follow:
<button data-assigned-id="@IdUser" onclick="updateClick()" type="button" class="btn btn-sm btn-default"></button>
My div
<div id="partial_load_div">
</div>
Script
function updateClick() {
var id = $(this).data('assigned-id');
$('#partial_load_div').show();
$('#partial_load_div').load('/Users/UpdatePartial?id=' + id);
}
The id is always shows as undefined, i checked and @IdUser
has always value
then in chrome dev i got the error
GET http://localhost:19058/Users/UpdatePartial?id=undefined 400 (Bad Request)
Any idea how to fix this?
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