0

How can I send $("#query").val()) using my Ajax function ?

If I put my Ajax call in my $(document).ready(function() , my code doesn't work anymore (the script doesn't start). => I can see the 'test123' string on my next page but , but if I type something in my "query" Input_Field, and then click on my link href (pointing to the same location) , the input field is reset and loose my value "query"... Please help me :( Thank you

$(document).ready(function() {

$("#completed").live('click', function() {
alert($("#query").val());

});

  $.ajax ({ 

url: 'http://localhost:3000/user/updateattribute',
data: { chosenformat: 'test123' , query: $("#query").val() } ,
type: 'POST',
success: function()
{
    alert ('success ' );
    return false;   }
});

});

user2567674
  • 171
  • 1
  • 3
  • 15
  • Does it alert anything? – Sterling Archer Feb 21 '14 at 21:47
  • If you refresh the page by reloading location, you lose all data filled in inputs. Is it your issue/question? You seem to ask two different questions here. Could you make your post clearer? – A. Wolff Feb 21 '14 at 21:48
  • @RUJordan yes it alerts my input_field value, but then I want to see it in my controller (so I need to pass it to my Ajax call) Trouble is that .ready() and .ajax() are separate as well as their variables are (unless we make them global ? but that's ugly I guess ^^) A.Wolff Let me know if that's still not clear enough. If not I'll make it big detailed tomorow! :) – user2567674 Feb 22 '14 at 00:50

1 Answers1

1

// do not use this anymore $(document).ready(function() {

$(function() {
       event.preventDefault();
       // live is no longer used use on..
       $("#completed").on('click', function() {
       console.log($("#query").val()); 
       // alerts are annoying learn to use console  
});
alexmac
  • 239
  • 2
  • 9
  • https://api.jquery.com/live/ see the note As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live(). – alexmac Feb 21 '14 at 21:55
  • it depends on 1. the version of jQuery used. Also, it would only eliminate the alert, the ajax would still work, and `$(document).ready()` is not deprecated either, what you used is just a shorter way of writing the same code. – Sterling Archer Feb 21 '14 at 22:04
  • only the .live is deprecated , I agree. Just shorter less, typing and if you had a loop with lots of data , alerts can DOS your own code. – alexmac Feb 21 '14 at 22:08
  • alerts is for debugging nothing more :) I'll try it out with .on instead of .live - cheers ! – user2567674 Feb 22 '14 at 00:52
  • I could use 'bind' with JQuery 1.4.3, that's fine : http://stackoverflow.com/questions/10912346/uncaught-typeerror-object-object-object-has-no-method-on] I'm afraid that doesn't help me on the Ajax call ;) – user2567674 Feb 22 '14 at 17:38