arnausd
arnausd

Reputation: 25

How to put variables in url

I want to add nameInput variable asked previously to the url to search it, its possible?

<html>
    <head>
        <script id="jsbin-javascript">
            var names = [];
            var nameInput = [ff];

            function insert() {
                alert(nameInput.value);
            }
        </script>
    </head>
    <body>
        <form>
            <input id="name" type="text" placeholder="Nombre" />
            <input type="button" value="Mostrar" onclick="insert()" />
        </form>
        <a href="http://reser.com" &+nameInput ">link text</a>
    </body>
</html>

Upvotes: 0

Views: 107

Answers (4)

user3589620
user3589620

Reputation:

Separate HTML and JavaScript:

HTML

<input type="text" id="inputUser" value=""/>
<a id="outputUser">Go to user page</a>

Add to <a> an click event:

a.addEventListener("click", function() {});

Get the input value:

var user = document.getElementById("inputUser").value

append to url:

a.href = url + input;

Complete JavaScript

var a = document.getElementById("outputUser");

a.addEventListener("click", function() {
    var user = document.getElementById("inputUser").value;
    a.href = "http://reser.com/" + user;
});

<script> goes in <body> after your HTML:

<body>
    HTML
    <script></script>
</body>

Upvotes: 1

sViz
sViz

Reputation: 3

By giving your anchor tag an id, e.g. "test", you can pass the input value to its href attribute.

function insert ( ) { 
    var newName = document.getElementById("name").value;
    document.getElementById("test").setAttribute("href","http://reser.com/" + newName);

    }

Upvotes: 0

groenborg
groenborg

Reputation: 65

I would suggest you do something like this, give the anchor tag and id and retrieve it with javascript, and add the href with you desired value

<a id="unique"href="http://reser.com"&+nameInput">link text</a>

document.getElementById("unique").setAttribute("href", yourDesiredValue);

it might be possible you will have to use

getElementsByTag("a")[0]

where 0 will be the number of the anchor-tag on the page

Upvotes: 0

Ailef
Ailef

Reputation: 7906

You can modify the window.location.href property to redirect the user to the new URL.

window.location.href = window.location.href + nameInput

If you want the user to click you can bind the insert() on the onclick event of the link and then make the redirect.

Upvotes: 0

Related Questions