0

I'm in the process of learning jQuery and feel that I must be missing something. I have a super simple form set up as below:

<form name="testform" id="testform" method="post">
    <select type="select" id="searchid">
        <option value="1">Option 1</option>
        <option value="2">Option 2</option>
    </select>
    <input type="text" id="searchtext" value="$_POST['value']">
</form>

I'd like to use jQuery to automatically submit the form when #searchid is changed. I understand how to do this with basic JavaScript, but I need jQuery for other things and need to learn how these things work. The below jQuery snippet seems to me like it should work, but it's not working. Any ideas why?

$( "#searchid" ).change( function() {
    $( "#testform" ).submit();
})
9
  • 1
    You are missing the quotes. Commented Jun 1, 2014 at 20:28
  • Thanks for pointing that out. I added them and it still doesn't work. :( Commented Jun 1, 2014 at 20:29
  • 3
    Have you put the code in document ready handler? Commented Jun 1, 2014 at 20:30
  • 2
    I'd suggest starting where we all started out, by reading the documentation, and jQuery now has it's own learning center. An hour of reading and trying some of the examples, and you should be able to make that code work. Commented Jun 1, 2014 at 20:32
  • 1
    Your inputs have no name, what are you trying to post? Probably what you get server side isn't really what you're waiting for. BTW what do you mean with "it doesn't work"? What do you get? What do you expect? Commented Jun 1, 2014 at 20:37

1 Answer 1

2

This is how you can submit the form:

$(function() {
    $( "#searchid" ).change( function() {
        $( "#testform" )[0].submit();
        //or this.form.submit();
    });
});

Quite close indeed. To make sure that your server-side will receive proper data, your inputs should have names.

EDIT

The code has been updated to include DOM ready. It's easy to assume that DOM ready is so fundamental and that it may not be an issue. However, it's never safe to make any assumptions.

Please note that when creating demos on js fiddle, you may not be required to use DOM ready if the drop down under jQuery version selector says 'onLoad' or 'onDomReady' ... .. as your code will be wrapped in the appropriate event handler by js fiddle anyway.

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

5 Comments

It doesn't work unless I wrap it in the ready handler. It seems strange to me that the examples I'm seeing don't require that.
DOM ready is a given, and your comment just came when I thought of including DOM ready. See the updated version. Where is the example? On JS Fiddle?
I was using the information from the "Learn jQuery" site referenced above well before asking this question. learn.jquery.com/events/handling-events Knowing that these handlers require DOM ready is good, but it would have taken me ages to find that information without this question. :/
@RMac: Seems like you skipped the introduction: learn.jquery.com/about-jquery/how-jquery-works
I didn't skip that. I just didn't understand it. I've been using event handlers in the HTML like onchange before and am new to all this. I didn't make the connection reading that that the HTML onchange handler is loaded when the DOM is ready anyway. It was an oversight on my part.

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.