Hello friends i am trying to get the id of a button on click whith jquery and append it in the bottom of the href i try with .append but i don´t get
i have this
<button class="btn1" id="5" type="button">click me</button>
<button class="btn1" id="3" type="button">click me</button>
<a href="www.example.com/{id]" class="dellink">Delete</a>
this works but i only need to get the id of the botton
$("btn1").click(function(){
$(".delllink").attr("href", "http://www.example.com/");
});
You could do it this way, if you want to keep the url from the element.
$("button.btn1").click(function() {
var url = $("a.dellink").attr("href");
url = url.substr(0, url.lastIndexOf("/") + 1) + $(this).attr("id");
$("a.dellink").attr("href", url);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn1" id="5" type="button">click me</button>
<button class="btn1" id="3" type="button">click me</button>
<a href="www.example.com/{id}" class="dellink">Delete</a>
Otherwise you can do it more static too:
$("button.btn1").click(function() {
$("a.dellink").attr("href", "http://www.example.com/" + $(this).attr("id"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn1" id="5" type="button">click me</button>
<button class="btn1" id="3" type="button">click me</button>
<a href="www.example.com/{id}" class="dellink">Delete</a>
Hope this will work for you:
$('button').click(function() {
var id = $(this).attr('id');
$("a").prop("href", "www.example.com/"+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