user2271933
user2271933

Reputation: 491

Get value from new input element on page load

I have a problem in getting the value from a dynamic added input field. I have a function that does a ajax call and in the end I add a input field like this:

$('<input type="text" id="col_1" value="done" hidden="true"/>').appendTo("#progress-category");

This part works just well, but my problem comes when on another function I need to get the value from my input on page load.

So when my page load I need to get the value check if it's equal to "done" and do some other stuff.

The usual way : $("#col_1").val() on alert() prints undefined.

I read about the .on function for dinamically added elements, but I don't want to bind the getting value phase on an certain even, I need that value as soon as my page is loaded.

Upvotes: 0

Views: 1718

Answers (1)

Georgi Naumov
Georgi Naumov

Reputation: 4211

One of possible solutions is to trigger event when you have added this input

Eg:

$('<input type="text" id="col_1" value="done" hidden="true"/>').appendTo("#progress-category");
$(window).trigger("inputIsadded");

After this you can add listener to "inputIsadded" event.

Eg.

$(window).on("inputIsadded", function() {
    // working with input 
});

Other approach is to use deferred object.

Upvotes: 1

Related Questions