Call the onClick function and pass it the value from the element's data- * attribute
I have links like this:
<li><a href="#" data-value="Codes">Get codes</a></li>
<li><a href="#" data-value="Product">Get products</a></li>
How can I do this so that when the link is clicked, then a function is called passing the value contained in the data value?
function refreshGrid(value) { }
+3
source to share
3 answers
Try the following:
$('li > a[href="#"][data-value]').click(function (e) { // On click of all anchors with a data-value attribute and a href of # that are immediate children of li elements (nb this selector can be modifed to suit your needs)
e.preventDefault(); // prevent the default (navigate to href)
refreshGrid($(this).data('value')); // call your refreshGrid function with the value contained in the 'data-value' attribute
});
+4
source to share