Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery anchor preventDefault

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>
like image 496
oshirowanen Avatar asked Sep 24 '10 12:09

oshirowanen


People also ask

What is e preventDefault () in jquery?

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.

How does anchor tag prevent default Behaviour?

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.

How do you make an anchor tag with nothing?

To make an anchor tag refer to nothing, use “javascript: void(0)”.


1 Answers

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();     
});
like image 51
Praveen Prasad Avatar answered Sep 24 '22 03:09

Praveen Prasad