user1958022
user1958022

Reputation: 39

Set textbox value into <a href> automatically

I found this code searching in the site, I would like to know how can I make this code work skipping the part to click generate button, so instead the text shows directly for download option when text-field is filled: jquery:

$(function(){

$('#gen').click(function(e){
    e.preventDefault();
    url='http://'+$('#address').val();
    $('a').attr('href', url);
});

});

html:

<input type='text' value='' />
<label id='gen'>Generate</label>
</br></br>
 <a href=''>Download</a>

DEMO

Upvotes: 0

Views: 963

Answers (3)

random_user_name
random_user_name

Reputation: 26150

All you need to do is switch from click event on the button input to keyup event on the text input:

First, you should add a name or id to the input, so we can identify it:

<input type="text" name="address" id="address" value="" />
<label id="gen">Generate</label>
</br></br>
<a href=''>Download</a>

Then, modify the jQuery:

$(function(){
    $('#address').keyup(function(e){
        url='http://'+$(this).val();
        $('a').attr('href', url);
    });
});

Working Demo: http://jsfiddle.net/pkq7H/

Upvotes: 2

Smeegs
Smeegs

Reputation: 9224

You can do it on the change event of the textbox

http://jsfiddle.net/hNsm5/157/

I updated the fiddle to show what I mean.

Instead of the click event it's now a change event for the textbox.

$('#address').change(function(e){

Upvotes: 1

Baximilian
Baximilian

Reputation: 885

All you need it's set onchange function

$(function(){

 $('#address').on("change",function(e){
     e.preventDefault();
     url='http://'+$('#address').val();
     $('a').attr('href', url);
 });

});

http://jsfiddle.net/hNsm5/155/

Upvotes: 2

Related Questions