tags:

views:

38

answers:

1

Hi, when user insert text input i wish get, and insert in link. How that make with jQuery?

My try (very bad :( ):

<script type="text/javascript"> 
$(document).ready(function() {
    var values = $('#user').val(); 
});
</script>
<body>
<div id = "table">
    <input type = "text" id = "user" value = "my_name" />
</div>
<a href = "http://www.link.com/?name=&lt;script&gt;values&lt;/script&gt;"&gt;link&lt;/a&gt;
+4  A: 
$(function() {
    // listen for the change event on the textbox
    $('#user').change(function() {
        // when the user changes the value of the textbox
        // get the new value
        var name = this.value;

        // and put this value in the link
        $('a#mylink').attr('href', 'http://www.link.com/?name=' + name);
    });
});

where the anchor is defined like so:

<a href="http://www.link.com/?name=my_name" id="mylink">link</a>​

You may see this in action here.

Darin Dimitrov
Use `this.value` instead of `$(this).val()` so you don't create another jQuery object...
Yi Jiang
@Yi Jang, yes that's a good suggestion. I will update my answer.
Darin Dimitrov
Yes, it works on the blur. If the user changes twice, when he clicks on the link the blur event will trigger and last value will be reflected in the href which seems logic.
Darin Dimitrov
@Darin - Oh right. I didn't think about the comment you made on @Bozho's answer.
ShiVik