简体   繁体   中英

change link href attribute with html() function

If i have a link in my page.I want to change its href attribute after some event is triggered how i can do that

Button

<a id="btn-start" href="/dashboard/save/{{ pk }}" class="btn btn-primary btn-embossed">Save Reward</a>

Javascript

$( "#sortable1 div" ).sortable({
        connectWith: "div",
        stop: function( event, ui ) {

            if($('#sortable2').find('img').length==6) {
                $('#btn-start').html("<a id='btn-start' href='/dashboard/redeem/{{ pk }}' class='btn btn-danger' >Redeem Coupon</a>");
            }
        }
    });

html() will change the inner HTML of your element. As a result, your code adds another <a> element inside your #btn-start element.

You can use the prop() function to change the actually HTML property (and attribute):

if($('#sortable2').find('img').length == 6) 
{
    $('#btn-start').prop('href', '/dashboard/redeem/{{ pk }}');
}
$('#btn-start').attr ('href', "http://sample");

As per your code you change href,class and innerhtml of "#btn-start".If its below is the working code

 $('#btn-start').attr('href=','/dashboard/redeem/{{ pk }}')
 .attr('class','btn btn-danger').html('Redeem Coupon');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM