javascript - Change input field value depending on what a link has -


i have link changing , want print text input text field depending on link points to. example link following.

<a onclick="main()" id="link" href="#foo"> 

or same link @ times like

<a id="link" href="#bar"> 

if link links to #foo want following like

<input id="input" type="text" value="hello world" /> 

and if link links #bar need input have value of bye world.

how can acheave this. tried following think doesn't work because url doesn't change until user clicks on it. looking isn't there yet.

function main() {   var url = window.location.href;   if (url.search("#foo") > 0) {     document.getelementbyid("link").value = "#foo";     document.getelementbyid("input").value = "hello world";   } } 

update here's list of of hash tags 1 link be. , input text field when links clicked.

#work = can't talk i'm @ work #home = have kids still can't talk #bar = i'm busy getting drunk can't talk #bathroom = listen perv i'm not interested don't it.  

but remember site located @ example.com has link can have of above hash tags links to. , when user clicks 1 link depending on links href has hash tag determines entered in input field.

this should work, can't test jsfiddle. helps understand.

js:

document.getelementbyid('link').onclick = function () {     var url = window.location.href;     if (url.indexof("#foo") > -1) {         document.getelementbyid("link").innerhtml = "#foo";         document.getelementbyid("input").value = "hello world";     } } 

html:

<a id="link" href="#foo">link</a> <input id="input"> 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -