1

What I am doing is whenever I click a li.opn I get the respective .pst 's html from #hiddenDiv .

If the .pst 's html is empty then do a $.post() to get the html from server.

But my problem is if I receive a <script></script> code from server in outputHTML then the document gets 2 scripts

  • One in displayDiv
  • Another in hiddenDiv

This causes various bugs like the script function runs twice.

UPDATE

Here's my My Problem Solution but i have to use the same html,js codes twice!! Any help ???

HTML

<ul>
   <li class="opn" set="stack">Stackoverflow</li>
   <li class="opn" set="google">Google</li>
</ul>


<div id="hiddenDiv">
 <div class="pst" set="stack"></div>
 <div class="pst" set="google"></div>
</div>
<div id="displayDiv"></div>

JS

<script>
$('.opn').click(function(){
var set = $(this).attr('set');
if($('.pst[set="'+set+'"]').html() > 0){
  $('#displayDiv').html($('.pst[set="'+set+'"]').html());
}else{
   $.post(url,{},function(data){
        var outputHTML = data.output; 
        $('#displayDiv').html(outputHTML);
        $('.pst[set="'+set+'"]').html(outputHTML);
      },'json');
 }
});
</script>

UPDATE2

If this is the output from server then 'asik was clicked' is showing 2 times

<div>
     <button class="asik">Click Me</button>
</div>
<script>$('.asik').click(function(){alert('asik was clicked');})</script>
Talha Awan
  • 4,573
  • 4
  • 25
  • 40
Slim Shady
  • 1,045
  • 2
  • 12
  • 38

1 Answers1

0

Try this: Remove script from server data and put that script following comment..

    if($('.pst[set="'+set+'"]').html() > 0){
      $('#displayDiv').html($('.pst[set="'+set+'"]').html());
    // server script put here..
    }else{
       $.post(url,{},function(data){
            var outputHTML = data.output; 
            $('#displayDiv').html(outputHTML);
            $('.pst[set="'+set+'"]').html(outputHTML);
    //// server script put here..
$('.asik').click(function(){alert('asik was clicked');});
          },'json');
     }
Ahosan Karim Asik
  • 3,219
  • 1
  • 18
  • 27