Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

append on click id from button jquery

Tags:

jquery

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/");
}); 
like image 390
htmlpower Avatar asked Jan 29 '26 15:01

htmlpower


2 Answers

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>
like image 105
eisbehr Avatar answered Jan 31 '26 04:01

eisbehr


Hope this will work for you:

$('button').click(function() { 
    var id = $(this).attr('id');
   $("a").prop("href", "www.example.com/"+id);
});
like image 27
Sunny Avatar answered Jan 31 '26 04:01

Sunny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!