2

I have div with generated input fields. After button click I want to refresh div and then set values.

<div>
  <input id="1" name="1" type="text" />
  <input id="2" name="2" type="text" />
</div>
<button type="button" onClick="change();">Change</button>

Javascript function:

function change(){
  $('div').load(' div');
  $("input[type='text']").val("some text");            
}

Why after load I can't set values for input? How can I solve it? Thanks

2
  • You could just call change() on load of the DOM...? Also, what are you expecting load(' div') to do, as it's not a valid usage of the load() method. Commented Mar 11, 2016 at 14:25
  • I need to refresh div because count of input fields are generated by server. Commented Mar 11, 2016 at 14:28

1 Answer 1

2

.load() is asynchronous. So when you do this:

$('div').load(' div');
$("input[type='text']").val("some text");

That second line executes before the content has been loaded. In order to respond to the completion of the content being loaded, you need to use the callback function:

$('div').load(' div', function () {
    $("input[type='text']").val("some text");
});
Sign up to request clarification or add additional context in comments.

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.