I am trying to set the value of a js variable when clicking a link.
This is what I have attempted so far (simplified):
<a href="#?id=1" value="1" class="delete_link">Click to delete this row</a>
$(document).ready(function(){
$(".delete_link").click(function(){
var deleteID = $(this).val();
alert(deleteID);
});
}):
But this does not set the variable deleteID
to 1. Am I selected the data in the link incorrectly?
Heres a jsfddle: link
I am trying to set the value of a js variable when clicking a link.
This is what I have attempted so far (simplified):
<a href="#?id=1" value="1" class="delete_link">Click to delete this row</a>
$(document).ready(function(){
$(".delete_link").click(function(){
var deleteID = $(this).val();
alert(deleteID);
});
}):
But this does not set the variable deleteID
to 1. Am I selected the data in the link incorrectly?
Heres a jsfddle: link
Try:
var deleteID = $(this).attr("value");
DEMO: http://jsfiddle/CjS8k/3/
value
isn't a valid attribute for <a>
elements, meaning it won't be put into the .value
property (which is what .val()
will return). So instead, use:
<a href="#?id=1" data-value="1" class="delete_link">Click to delete this row</a>
with:
var deleteID = $(this).attr("data-value");
DEMO: http://jsfiddle/CjS8k/4/
there "value" is an attribute.
change your line to
var deleteID = $(this).attr("value");