Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to update a url link based on user text input?

For instance, I know that it is possible to do something in Javascript that allows users to update text based on user text input:

<script type="text/javascript">
function changeText2(){
var userInput = document.getElementById('userInput').value;
document.getElementById('boldStuff2').innerHTML = userInput;
}
</script>
<p>Welcome to the site <b id='boldStuff2'>dude</b> </p> 
<input type='text' id='userInput' value='Enter Text Here' />
<input type='button' onclick='changeText2()' value='Change Text'/>

View the above code in action at: tizag.com/javascriptT/javascript-innerHTML.php

However, instead of the above code, I would like to know if it's possible to do something similar for a url link. Below I've placed a step by step example of what I would like to happen upon the user inputing text:

  1. Original Link: http://www.google.com/search?q=

  2. User Input in Text Field: espn

  3. User clicks button to submit text in text field

  4. Final Link: http://www.google.com/search?q=espn

Thanks for your help...BTW...if you can't tell I'm a bit of a novice so detail is appreciated.

like image 498
Doug Avatar asked Aug 17 '11 18:08

Doug


People also ask

How do you input text as a hyperlink in HTML?

The <input type="url"> defines a field for entering a URL. The input value is automatically validated before the form can be submitted. Tip: Always add the <label> tag for best accessibility practices!

How do I change the text of a link using Javascript?

Use the textContent property to change the text of a link element, e.g. link. textContent = 'Replacement link text' . The textContent property will set the text of the link to the provided string, replacing any of the existing content.

What does input type URL do?

Input Type Url The <input type="url"> is used for input fields that should contain a URL address. Depending on browser support, the url field can be automatically validated when submitted. Some smartphones recognize the url type, and adds ".com" to the keyboard to match url input.


2 Answers

Here's one in plain JS that updates as you type:

<a id="reflectedlink" href="http://www.google.com/search">http://www.google.com/search</a>
<input id="searchterm"/>

<script type="text/javascript">
    var link= document.getElementById('reflectedlink');
    var input= document.getElementById('searchterm');
    input.onchange=input.onkeyup= function() {
        link.search= '?q='+encodeURIComponent(input.value);
        link.firstChild.data= link.href;
    };
</script>

Note:

  • no inline event handler attributes (they are best avoided);

  • assigning both keyup and change, to try to get keyboard updates as they happen and ensure that all other updates get caught eventually;

  • the use of encodeURIComponent(), necessary in case the search term has any non-ASCII or URL-special characters in;

  • setting the search property of a Location (link) object to avoid having to write out the whole URL again;

  • setting the data of the Text node inside the link to reflect the full URL afterwards. Don't set innerHTML from user input as it may have HTML-special characters like & and < in.

like image 128
bobince Avatar answered Sep 22 '22 20:09

bobince


#1: you need some forms
#2: you need to catch when the form is submitted
#3: based on the form's submission change the url

Here is a demo: http://jsfiddle.net/maniator/K3D2v/show/
here is the code: http://jsfiddle.net/maniator/K3D2v/embedded/

HTML:

<form id="theForm">
    <input id='subj'/>
    <input type='submit'/>
</form>

JS:

var theForm = document.getElementById('theForm');
var theInput = document.getElementById('subj');

theForm.onsubmit = function(e){
    location = "http://jsfiddle.net/maniator/K3D2v/show/#" 
                              + encodeURIComponent(theInput.value);
    return false;
}
like image 43
Naftali Avatar answered Sep 25 '22 20:09

Naftali