I have a html button in a web page. When I click the button, I’d like to redirect to another page.
I have this code :
<button id="btn" type="submit" style="width: auto">
<div style="height: 40px">
<img alt="Button" src="/images/somepicture.png" style="float: left" />
<span style="margin: auto">Label</span>
</div>
</button>
<script language="javascript" type="text/javascript">
$(function () {
$("#btn")
.mousedown(function (event) {
window.location.href = "otherpage";
event.preventDefault();
});
});
</script>
However, this code is not behaving as expected. The page is simply posting, and the redirection does not occurs.
What is wrong with my code?
This should work for you:
Note: Text and images are the only valid contents for a
<button>. I don’t think this would be the cause of your issue, but it is best to follow the standards.