Redirecting to another page when the button is clicked using jQuery

Bugwee
Posted by Bugwee under jQuery category on | Points: 40 | Views : 18082
the button
  <input id="btnID" type="button" value="button" />



$('#btnID').click(function() {
window.location = "http://dotnetfunda.com";
});

Comments or Responses

Posted by: Akiii on: 4/19/2011 Level:Bronze | Status: [Member] | Points: 10
@Bugwee....

Hi, i have used this code but it isnt working !
When i press the button, the page refreshes but nothing happens......

Please tell me why is this happening...?

Thanks and Regards
Akiii
Posted by: Bugwee on: 4/19/2011 Level:Starter | Status: [Member] | Points: 10
hi Akii,

actually if you are using the asp button, the page just refreshes and nothing happens,
<asp:Button ID="btnID" runat="server" Text="Button" />


to get it working i used the html button
 <input id="btnID" type="button" value="button" />


i don't know yet on how or why it doesn't able to function if using the asp button.


Posted by: Bugwee on: 4/19/2011 Level:Starter | Status: [Member] | Points: 10
hi Akiii,

im sorry... here it is, you may try this one

 <asp:Button ID="btnID2" runat="server"

Text="Button" />


 $("#<%=btnID2.ClientID %>").click(function() {

window.location = "http://dotnetfunda.com";
return false;
});


Regards,
Bugwee
Posted by: Akiii on: 4/19/2011 Level:Bronze | Status: [Member] | Points: 10
Hi Bugwee,
I have tried both the buttons but nothing is happening except the page refreshes....
have you tried this in your computer ??

Thanks and Regards
Akiii
Posted by: Bugwee on: 4/19/2011 Level:Starter | Status: [Member] | Points: 10
hi Akiii,

Yeah, I have tried it on my computer and it both works fine.

Regards,
Bugwee

Posted by: Akiii on: 4/19/2011 Level:Bronze | Status: [Member] | Points: 10
hi bugwee,
I am pasting my code here, please have a look at it :-

<head runat="server">

<title>My Form</title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
$("#<%=btnID2.ClientID %>").click(function () {

window.location = "http://dotnetfunda.com";

return false;

});

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnID2" runat="server" Text="Button" />
</div>
</form>
</body>


Thanks and Regards
Akiii
Posted by: Bugwee on: 4/19/2011 Level:Starter | Status: [Member] | Points: 10
hi Akiii,

you have have to do it like this,
<script type="text/javascript">

$(function($) {

$("#<%=btnID2.ClientID %>").click(function () {

window.location = "http://dotnetfunda.com";
return false;

});
});

</script>


Regards,
Bugwee
Posted by: Akiii on: 4/19/2011 Level:Bronze | Status: [Member] | Points: 10
Hi bugwee,

Excellent work bro...........its working like a breeze......
i guess $(function($) was not written earlier......
Its nice talking to you.......
Thank you very much for your help......

Akiii
Posted by: Bugwee on: 4/19/2011 Level:Starter | Status: [Member] | Points: 10
hi Akii,

i thought you already know that $(function($) .. LOL

Cheers,
Bugwee
Posted by: Akiii on: 4/19/2011 Level:Bronze | Status: [Member] | Points: 10
Hi,
No, i am new to jquery.....
but why is this $(function($) written ??

Akiii
Posted by: Bugwee on: 4/19/2011 Level:Starter | Status: [Member] | Points: 10
$(function($) {

// jQuery codes in here.
});

and
 $(document).ready(function() {

// jQuery codes in here.
});


works the same.. You may see here on how important it is..
http://docs.jquery.com/Tutorials:Introducing_$(document).ready()


Posted by: Akiii on: 4/20/2011 Level:Bronze | Status: [Member] | Points: 10
@bugwee......thanks for helping me .......
good link.....

Akiii
Posted by: Bugwee on: 4/20/2011 Level:Starter | Status: [Member] | Points: 10
hi Akiii,

your welcome.. we are all brothers in here..

Bugwee
Posted by: Akiii on: 4/20/2011 Level:Bronze | Status: [Member] | Points: 10
@bugwee.......thats right.....

keep up the good work.....

Akiii
Posted by: Bugwee on: 4/20/2011 Level:Starter | Status: [Member] | Points: 10
hi Akiii,

we will keep it up... we need to communicate, share, and learn with our fellow developers.

Thanks,
Bugwee
Posted by: Join2nidhi on: 6/24/2013 Level:Starter | Status: [Member] | Points: 10
Try this one


$(document).ready(function () {
$("#<%=btnID2.ClientID %>").click(function () {
alert("JKK");
window.location = "URL";

return false;

});
});


<asp:Button ID="btnID2" runat="server" Text="Button" />

Login to post response