1

1.This is my code here i have a div class inner which is dynamically loaded using ajax call and after ajax call if i click the hide button it is not working.

But its working perfectly before ajax request.

so in order to overcome i just add a outer div and hide that div this time it works.. I don't know why?

$( "#inner" ).replaceWith( data );  /*and*/  $( "#inner" ).hide();   //not working


$( "#inner" ).replaceWith( data );    /*and*/     $( "#outer" ).hide();  //working

Why we cant use the same div class ?

<html>
  <div id="outer">
  <div id="inner">
    <br /> <br /> <br />
  <div>  <input type="button" value="signup"  onclick="changeval();"/>
  </div>
  <br /> <br />  
  </div>
  </div>
  <input type="button" value="hide" onclick="onhide();"/>    
  <script language="javascript"> 
   function changeval(context)
   {          
     var typeval="sdsf";
     var url="sdfsdf";
     $.ajax({
        type:'POST',
        url:'htp://sscs/registration',
        data:'&typeval='+typeval+'&url='+url,
        success:function(data) { 
          $( "#inner" ).replaceWith( data );           
        }        
      });         
    }
    function onhide()
    {
      $( "#inner" ).hide();
    }
  </script>
3
  • Try using $("#inner").replaceWith($('#inner',data)); Commented Jan 13, 2014 at 9:55
  • try .html() instead of replaceWith() Commented Jan 13, 2014 at 9:58
  • yes it worked,but i want to know why its not working when we use replace Commented Jan 13, 2014 at 9:59

4 Answers 4

1

Use .html()

 $("#inner").html(data);

instead of .replaceWith() as

Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed.

DEMO of replaceWith, Here you can see div with second class is replace with input content.

Sign up to request clarification or add additional context in comments.

Comments

1

It doesn't work because you replace the <div id="inner">.

Including the div and its ID. <div id="outer"> remains so your other hide works, it still finds that div.

Comments

0

Use like this:

$( "#inner" ).replaceWith( function(){
    return $(this).data();
} );

Comments

0

After your ajax call the #inner-div does not exist anymore. You are replacing it with the response from your ajax request .

You could use $("#inner").html(data); to keep the div and then hide it once you received the response.

1 Comment

Wrong context, sorry. Sould be html().

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.