0

I'm trying to send a request to a PHP form (using get) that is dynamically generated based on a textbox field the user types in. I've tried a few things but can't manage to get the value of the textbox.

<script>
  $("#myDiv").on('click', 'p', function(){
      setInterval(function() {
        $.ajax({url:"val",success:function(result){
          $("#myDiv").html(result);
        }});
      },1000);
  });
</script>

If I leave it like this, it works and sends the request to "val", but I'm not sure how to put the value of the textbox in there. It is still within the same div, which is dynamically loaded after the page has been loaded completely.

Any help would be appreciated.

1
  • Question not yet clear. can you paste HTML also here? $("#ID").val() is used to get the value of an form element Commented Apr 9, 2013 at 3:25

1 Answer 1

1
var val = $("#id-of-textbox").val();

Replace #id-of-textbox with the actual id of your textbox and the var val will then contain the text value of your textbox.

You'll send it with your ajax request like so:

$.ajax({url:"val", data: { value: val }, success: function...
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the help, I can actually get SOMETHING now. However, instead of giving me the value of the textbox, it says [object Object] Also, I'm dynamically creating this textbox, and this only works for elements loaded with the page. How could you modify this to make it work after updating it. I've already used .on to create an onclick for a dynamic element, I'm just not sure how that would apply here.
Nevermind, I got the string value by getting rid of the {value: }
This should work for dynamically created elements, just make sure that the variable assignment code runs after the element has been created.
It does work for dynamic elements, just a small typo on my part, thanks for your help!

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.