0

I've had this problem for awhile now and I can't seem to fix it no matter what I do. Basically, my input is not retrieving the value that the user types in the input for some reason..

Here is my code:

$('#aid').one('click', function () { 
    $('.prompt').prepend('<tr class="task"><td class="cell-icon"></td>' +
        '<td class="cell-title"><div>User\'s Object: <input id="inputObject" type="text" style="margin-top: 2px;margin-left:2px"></input>&nbsp;&nbsp; Amount: <input id="inputAmount"' +
        'type="text" style="margin-top:2px;margin-left:2px; padding-right: 0px"></input></div></td>' +
        '<td class="cell-status hidden-phone hidden-tablet"><a class="btn btn-success" style="margin-top:3px">Submit</a></td>' +
        '<td class="cell-time align-right">Just Now</td></div>' +
        '</tr>');
});
$('.btn').click(function () {
    console.log("click");
    var input = document.getElementById('inputObject').value;
    console.log(input);
});

Everything works fine including both clicks, but for some reason it just won't display the input value to the console.

I've also tried: $('#inputObject').val(); but that didn't work either.

I really hope that someone can help me here!

4
  • 2
    Is the missing close quote on the first jQuery selector a typo or part of the problem? Commented Sep 6, 2014 at 14:35
  • What happens if you replace the console.log with an alert? Commented Sep 6, 2014 at 14:37
  • 1
    Replace $('#aid) by $('#aid') Commented Sep 6, 2014 at 14:39
  • 1
    no that was just a copy pasting error my bad. Commented Sep 6, 2014 at 14:51

2 Answers 2

1

Another method: use delegate.

$('body').delegate('.btn', "click", function() {
   var inp = document.getElementById("inputObject").value;
   console.log(inp); 
});

Explanation from http://www.w3schools.com/jquery/event_delegate.asp:

The delegate() method attaches one or more event handlers for specified elements that are children of selected elements, and specifies a function to run when the events occur.

Event handlers attached using the delegate() method will work for both current and FUTURE elements (like a new element created by a script).

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

4 Comments

As of jQuery version 1.7, the on() method is the preferred method for attaching event handlers for selected elements.
Yes, it's also specified in the link above. This is why I gave you a like, because you had a good solution. But, delegate has an advantage: the order of parameters. To my mind it's nicer to know which is the element, then which is the event.
Everybody has their own ways, to my mind it's nicer to use on() :)
I agree. Only for the order of parameters I like delegate, but on sounds better - it's more intuitive.
1

You are creating your HTML code dynamically so try using:

$(document).on("click",".btn", function(){
    do stuff here... 
});

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.