Using jQuery, how do I get the value from a textbox and then load a new page based on the value?
For example, lets say the textbox contains "hello" on page1.php, how do I change the default behavior of an anchor tag to now load the following
page2.php?txt=hello
I have the following so far:
<script type="text/javascript">
$(document).ready(function(){
$("a.mylink").click(function(event){
alert("link clicked");
event.preventDefault();
});
});
</script>
preventDefault() method stops the default action of an element from happening. For example: Prevent a submit button from submitting a form. Prevent a link from following the URL.
Using JavaScript – preventDefault() function To prevent an anchor from visiting the specified href, you can call the Event interface's preventDefault() method on the anchor's click handle. The code would look like this: JS. HTML.
To make an anchor tag refer to nothing, use “javascript: void(0)”.
Html
<input type="text" id="demoQuery" />
<a id='demoLink' href='javascript:'>Iam a link</a>
Javascript
jQuery('#demoLink').bind('click',function(e){
e.preventDefault();
document.location.href="someUrl.php?text="+ jQuery('#demoQuery').val();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With