2

I'm creating an application, with several forms that are loaded after an AJAX call.

Lets say my main files is the index.html. Then I have several forms that are loaded into the index.html with ajax call.

What I like to do is to check if a specific text input element is loaded with one of the forms and then change the value of that element. Is there a way to do that ?

For the events in example, there is the live() method that check if an item loaded later with ajax made some event and then executes some code. Can I do the same without event ? Can I add a value to that input text element after is loaded ?

2 Answers 2

5

You can check for its existence at regular intervals like so:

setInterval(function(){
    if($('input[name=my_input]').length > 0){
        $('input[name=my_input]').val('New Val');
    }
}, 200);
Sign up to request clarification or add additional context in comments.

2 Comments

There is no imidiate way with no function ?
If you have access to the AJAX call you load the form in with you can just use the complete function, but otherwise this method queries the browser at regular intervals to detect the element.
3

This is going to be a little pseudo code, as there is no markup supplied in the question ?

$.ajax({
    type: 'POST',
    url: 'someurl',
    etc....
}).done(function(data) {
    $(element).append(data); //insert form etc
}).always(function() {
    if ($("#inputID").length) {              //check if input element exists
        $("#inputID").val('some new value'); // could be done with a timeout to make sure the DOM inserting is done
    }
});

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.