0

I am kind of new to javascript however I have created a submit form that I want to redirect me to a url based on form input. Here is my current code...

The issue I'm running into however is that the form is sending me the initial value rather than the updated form value (It is using "whatevs" no matter what).

HTML

     <form id="Search-Form" onClick="genURL()"><label>Value: </label> 
     <input type="text" id="search" placeholder="Enter Value"></input>                           
     <div id="search-buttons">
         <input id="searchSubmit" value="whatevs" type="submit"  tabindex="1" />
     </div>
     </form>

Javascript

    function genURL() {
        var searchSubmit = document.getElementById("searchSubmit").value;
        window.location = "randomsite/view" + searchSubmit;
    }
1
  • onsubmit instead of onClick document.getElementById("search").value instead of document.getElementById("searchSubmit").value Commented Nov 4, 2014 at 0:40

1 Answer 1

1

Add return and use onsubmit:

 <form id="Search-Form" onsubmit="return genURL()"><label>Value: </label> 
 <input type="text" id="search" placeholder="Enter Value"></input>                           
 <div id="search-buttons">
     <input id="searchSubmit" value="whatevs" type="submit"  tabindex="1" />
 </div>
 </form>

Revise function like so:

function genURL()
{
    location.href = "randomsite/view" + document.getElementById("search").value;
    return false;
}

If you were to use onclick, it would go on the button, not the form.

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

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.