1

I'm trying to use an input to allow the end user to change the href of an a element using JavaScript, and I can't seem to get things working quite right.

Here's what I have so far...

<input type="text" id="feed" /> <a id="display" href="">#</a> 


$('#feed').keyup(function () { var temp = ""; temp = $('#feed').text.val(); $('#display').href = temp; }); 

Any help is much appreciated!

1
  • 2
    temp = $('#feed').val(); Commented Jun 21, 2016 at 15:35

2 Answers 2

5

Remove the .text that will throws error in console. To get the value you just need to use val() method on jQuery object.

Since $('#display') returns a jQuery object you need to use attr() to update an attribute (or use prop() and update href property) or you need to get dom object by $('#display')[0] and update href property.

$('#feed').keyup(function () { var temp = $('#feed').val(); $('#display').attr('href', temp); }); 
Sign up to request clarification or add additional context in comments.

1 Comment

Also, you could use $('#display').attr('href', temp) to assign the href if you were so inclined.
0

I assume the $ means you're using jQuery.

$('#feed').keyup(function () { var temp = ""; temp = $('#feed').val(); $('#display').attr('href', temp); }); 

1 Comment

He has also tagged it as jquery

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.