0

I am trying to build a simple "form" where you input a "from" address and a "to" address and hit "OK".

I want the script to take the "from" and "to" and a preset text "maps.google.com/dir/ from / to" and change the a href value to that link.

Here is what I got from similar tutorials:

    <script type="text/javascript">
function getlinks(){
    var from = document.getElementById('from').value;
    var to = document.getElementById('to').value;
    var lnkgm = document.getElementById('lnkgm');
    lnkgm.href = "https://www.google.de/maps/dir/" + from + "/" + to;
}
</script>
From: <input type='text' id='from' value='' /><br>
To: <input type='text' id='to' value='' /><br>
<input type='button' onclick='getlinks()' value='OK'/><p>
<a href="lnkgm"><img src="image.png"></a>

this kinda worked in a text version using a "innerHTML" but I want the script to update the

Any pointers?

Thank you so much!

1
  • You have no element with the id lnkgm So I am sure your developer console tells you that. Commented Oct 31, 2016 at 12:33

2 Answers 2

1

Just change the href attribute to id:

<a href="lnkgm">

To

<a id="lnkgm" href="#">

As you're trying to get the element by id (document.getElementById('lnkgm')) you have to set it's id attribute.

Demo:

function getlinks(){
    var from = document.getElementById('from').value;
    var to = document.getElementById('to').value;
    var lnkgm = document.getElementById('lnkgm');
    lnkgm.href = "https://www.google.de/maps/dir/" + from + "/" + to;
  console.log(lnkgm.href);
}
From: <input type='text' id='from' value='' /><br>
To: <input type='text' id='to' value='' /><br>
<input type='button' onclick='getlinks()' value='OK'/><p>
<a id="lnkgm" href="#"><img src="image.png"></a>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This is exactly the help I was looking for! :) It works perfectly.
0

You forgot to add the ID to your link:

<a id="lnkgm" href="lnkgm"><img src="image.png"></a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.