Robimp
Robimp

Reputation: 698

Pass Text Field Input To Javascript Variable - AJAX Codeigniter

I have a search form:

    <form id="global_search">
      <input id="global_search_input" name="search_query" placeholder="Enter search text">
      <input id="global_search_button" type="submit" value="" onclick="slide_box_search(search_query); return false;" class="listings_box">
    </form>

and I want to display the results in a slide down div, pulling the results in via AJAX. I'm using codeigniter, so I want to pass the search query to the controller (as a url segment). The ajax and controller bit all works fine using Jquery and the load function, however I don't know how to get the search query from the input box to the function in the submit onclick = slide_box_search(search_query)?

I'm thinking it's probably something like $(this).val() but can't quite figure it out.

Thanks in advance!

Upvotes: 0

Views: 2003

Answers (1)

Nick Craver
Nick Craver

Reputation: 630389

Instead of this you need the value from the other input, like this:

$('#global_search_input').val();

Or overall:

slide_box_search($('#global_search_input').val()); return false;

It would be better to attach the event handler unobtrusively though, like this:

$(function() { //run when the DOM is ready
  $("#global_search_button").click(function() {
    slide_box_search($("#global_search_input").val()); 
    return false;        
  });
});

...and just remove your inline onclick altogether.

Upvotes: 4

Related Questions