jquery - Can it possible injecting javascript to dom which is loaded after using ajax call? -


i have div id #id1 contains links. whenever click on links, make ajax call returns other links server.

now doing replacing old links new ones in #id1 using jquery replacewith() method. doing so, maintain id of div #id1.

what trying achieve here bunch of links server clicking in newly occupied links on div #id1 , continuously so.

is possible write script that?

<div id = "id1"> <ul> <li><a href ="test/show/1">link1</a></li> <li><a href ="test/show/2">link1</a>link2</li> .... </ul> </div>  <script>    $('#id1 a').on(click, function(){      $.ajax({             url: '/test/test_now?page=' + page,             datatype: 'json',             success: function(response) {                    $('#id1').repalcewith(response.links);             },             error: function() {                  alert('an error occurred');             }         });</pre> 

as pointy said, think need more specific, think job templating, 1 of favorites (for small size) template7.

you create template in script tag in foot of html:

<script id="template" type="text/template7">   <ul>     {{#each links}}     <li>       <a href="{{url}}">{{text}}</a>     </li>     {{/each}}   </ul> </script> 

and jquery or js

var template = $('#template').html();  // compile template7 var compiledtemplate = template7.compile(template);  // may render our compiled template passing required context var context = {     links:[         {text:'text',url:'http://www.example.com'}     ] };  var html = compiledtemplate(context); $('#id1').html( html ); 

you can include context wherever want , each time receive new links, update context , reuse compiled template , replace html again in "#id1" div don't forget include template7's js script file in html

regards


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -