Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How get input text field from HTML into JavaScript and go to URL?

I'm building one WebPage where you type some word into the input field and the Java gets this string and check if this string is equal to another, if it is go to some URL.

My code is:

 <input type="text" name="procura" id="procura" />
  <script>
  name = oForm.elements["name"].value;

  if (name.equals("Advogados"))
 {
     window.location = "http://jornalexemplo.com.br/lista%20online/advogados.html"
     //do something
 };
  </script>

Can you give me some lights?

share|improve this question
2  
I'm guessing this is javascript, not java – DaveHowes Jun 18 at 15:06
1  
1) 'Java' is to 'JavaScript' as 'Car' is to 'Carpet'. 2) Please add an upper case letter at the start of sentences. Also use a capital for the word I, and abbreviations and acronyms. This makes it easier for people to understand and help. – Andrew Thompson Jun 18 at 15:08

2 Answers

up vote 0 down vote accepted

Note, I've used the jquery library here in my example as it makes setting up the listeners to handle these events easier.

You're referencing oForm in your code, but I don't see that in your examples... so I would think that you will find this easier if you wrap the in a form tag, with a particular id (procura)

<div>
    <form method="get" id="procura">
        <input type="text" name="procura" id="procura_texto"  placeholder="Procurar"/>
    </form>
</div>

Then capture the result using the id of the input element (procura_texto), and jquery's val() method and prevent the form from being submitted using the method preventDefault():

$("#procura").on("submit", function(event){     

    // prevent form from being submitted
    event.preventDefault();

    // get value of text box using .val()
    name = $("#procura_texto").val();

    // compare lower case, as you don't know what they will enter into the field
    if (name.toLowerCase() == "advogados")
    {
        // redirect the user.. 
        window.location.href = "http://jornalexemplo.com.br/lista%20online/advogados.html";
    }
    else
    {
        alert("no redirect..(entered: " + name + ")");
    }
});

here's a jsfiddle for you to play with: http://jsfiddle.net/zwbRa/5/

share|improve this answer
it´s almost there. the problem is when i try to fit the code in my web page it just like do some refresh.. – user2497695 Jun 19 at 12:29
are you using jquery? jquery.com – msturdy Jun 20 at 8:07
sounds like you're not including the jquery library in the head of your page.. check out this page: jquery.com/download either download the library and install it locally, or use one of their CDNs! – msturdy Jun 20 at 9:43

Use window.location.href = "your url".

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.