0

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>");
            }
        }
    });
Binit Singh
  • 973
  • 4
  • 14
  • 35
  • 1
    Possible duplicate: http://stackoverflow.com/questions/179713/how-to-change-the-href-for-a-hyperlink-using-jquery – Rahul Desai Jan 29 '14 at 11:21

3 Answers3

1

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 }}');
}
BenM
  • 52,573
  • 26
  • 113
  • 168
0
$('#btn-start').attr ('href', "http://sample");
Prashobh
  • 9,216
  • 15
  • 61
  • 91
0

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');
P.Sethuraman
  • 705
  • 3
  • 5