IgorZ
IgorZ

Reputation: 1164

jQuery find id after loading

Here is my structure:

Person.html

...
<script src="../../js/site.js"></script>
...
<a id="findPersonById" href="javascript:void(0);" class="btn btn-primary">
Find person by id</a>
...
<div id="person-result">results div</div>

Site.js

$(document).ready(function () {
  privateFunction();
});
...
$('#findPersonById').click(function () {
  $("#person-result").load('/person/find #inside-container');
});
...

/person/find

<script src="../../js/site.js"></script>
...
<div class="container">
<div id="inside-container">
    <br/>
    <form id="personFindByIdForm">
        <div class="form-group">
            <div class="input-group">
                <span class="input-group-addon">PERSON'S ID</span>
                <input type="text" class="form-control" name="id"/>
                <span class="input-group-btn">
                    <button class="btn btn-default" type="submit">Find</button>
                </span>
            </div>
        </div>
    </form>
    <div id="find-result">res-div</div>
</div>

So, first of all I'm finding #findPersonById and loading /person/find #inside-container in the #person-result. It works. My next step was about to find two ids #personFindByIdForm and #find-result. I can't do it since document is already loaded if I'm right. How could I do this ?
And the second question - if I add any js code into the div that will be loaded into another div, will that js code run (why is it not running)? Like:

$("#div-2").load('/any-url #div-1');
<div id='div-1'>
  <script>console.log('Any JS')</script>
</div>

Thank you.

Upvotes: 3

Views: 1952

Answers (1)

renakre
renakre

Reputation: 8291

You can make sure the items are loaded this way:

$("#div-2").load('/any-url #div-1', function(){
     //here all the items loaded will be accessible

});

Upvotes: 5

Related Questions