0

I want to change the href of a button:

<button class="btn btn-info btn-lg" id="home" onclick="location.href='index.php'">Home</button>

I tried this:

$('#home').prop("href" , "home.php");

PHP:

<button class="btn btn-info btn-lg" id="home" onclick="location.href='<?php echo $link; ?>'">Home</button>

but it didn't work.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Prashant
  • 1
  • 2

3 Answers3

0

Wrap #home around an anchor <a> tag then you can change it like so:

Edit: As @putvande's comments below suggest that I cannot have a button inside an anchor tag since it's semantically incorrect, I will change the <button> tag to a <span> with class btn.

<a href="<?php echo $link; ?>">
    <span class="btn btn-info btn-lg" id="home">Home</span>
</a>

<script type="text/javascript">
$('#home').parent().attr('href', 'home.php');
</script>
0

Try this

$("#home")[0].onclick = null;
$("#home").click(function() { location.href="home.php";});
void
  • 36,090
  • 8
  • 62
  • 107
-1

I think what you're looking for is attr() not .prop()

JS:

$('#home').attr("href" , "home.php");

See fiddle:

https://jsfiddle.net/epL9gb6t/

Phillip Chan
  • 993
  • 7
  • 17