2

I have a page with an input field where I'd like to send the user to the URL they enter in the input, with -.html appended at the end. My HTML is as follows:

<form name="codeform"> 
<input type="text" id="code" name="code" autofocus>
</form>

<a href="#" class="button" id="submit">SUBMIT</a>

And my javascript:

$('#submit').click(function () { location.href = window.document.codeform.code.value + '.html'; });

When the user clicks the button, the script works as intended and the user goes to the page. But I'd also like the script to execute when the return key is pressed. Right now, when the return key is pressed, it tries to submit the form and I end up with a query string.

What is the best way to send the user to the page whether the button or return key is pressed?

5
  • Instead of having $('$submit').click(....), add the 'action' attribute to the form. Commented Jan 25, 2017 at 3:23
  • stackoverflow.com/questions/18545941/… Commented Jan 25, 2017 at 3:30
  • Thanks Riya! Seems obvious now but I couldn't figure it out for the life of me! Commented Jan 25, 2017 at 3:39
  • why u don't use submit button inside the form? w3schools.com/html/html_forms.asp Commented Jan 25, 2017 at 3:42
  • Yeah, i switched to a submit button. Commented Jan 25, 2017 at 4:17

3 Answers 3

2

Because your a.button is outside from the form, you need to add trigger to the form and the a.button too. Then just detect the Enter keycode, and done.

$('form').submit(function(e) {
  e.preventDefault();
  console.log(window.document.codeform.code.value + '.html')
});
$('form').keydown(function(e) {
  if (e.which == 13) {
    console.log(window.document.codeform.code.value + '.html')
  }
});
$('#submit.button').click(function() {
  console.log(window.document.codeform.code.value + '.html')
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<form name="codeform" id="codeform"> 
    <input type="text" id="code" name="code" autofocus>
</form>

<a href="#" class="button" id="submit">SUBMIT</a>

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

Comments

0

You can add a keydown listener that only runs on enter (keycode 13), and then prevents the default form submission action:

$('#submit').click(function () { location.href = window.document.codeform.code.value + '.html'; });
$('#code').keydown(function (event) {
    if(event.which==13){
        event.preventDefault();//prevents form submission
        location.href = window.document.codeform.code.value + '.html';
    }
});

Comments

0

Since you have the logic attached to the button click() event, you can simply attach a keypress() event to the input box to subsequently execute the button click.

$("#code").keypress(function(event) {
    if (event.which == 13) {
        event.preventDefault();
        $('#submit').click();       
    }
});

However, for tidiness I usually prefer to move the logic from the click event into a separate function and call that one function from both the click() and keypress() events.

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.