Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete dom element from jquery object

i want to delete a particular class from my object as my requirement is to delete that dom data before displaying content. I have written a sample code but not able to get why that is not working. I jquery's remove is also not working. Please help me to get it solve. Thanks in advance

<html>
<head>
<title>test</title>
<script type="text/javascript" src="jquery-1.5.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {

    // complete html
    var test;
    test = $('#issue_detail_first_row').html();

    var x = $(test).find('#issue_detail_nav').not('.p1');

    $('#sett').html(x);
 });

</script>
 </head>
 <body>
 <div id="issueDetailContainer">
        <div  id="issue_detail_first_row">
            <div>
                <div id="issue_detail_nav">
                    <div>test</div>
                    <div id="gett">
                        <div class="p1">
                            this content need to be deleted 1
                        </div>
                    </div>

                    <div class="p1">
                        this content need to be deleted 2
                    </div>

                </div>
            </div>                
        </div>
        <br/><br/><br/><br/>
<div id="sett">
</div>

like image 587
Rahul Avatar asked Jul 14 '11 15:07

Rahul


3 Answers

You need to remove the content from the DOM directly.

$("#issue_detail_first_row .p1").remove();

That will select the .p1 elements and remove them from the DOM

like image 106
Bobby Borszich Avatar answered Oct 07 '22 01:10

Bobby Borszich


you can use remove function on javascript object.

If you want to preprocess it before displaying.

example

var a =$("#issue_detail_first_row").html();
var jhtml =$(a)[0];   
$(jhtml).find('.p1').remove();
alert($(jhtml).html());

now use jhtml . demo

http://jsfiddle.net/WXPab/14/

like image 26
Vivek Goel Avatar answered Oct 07 '22 01:10

Vivek Goel


It seems that you're trying to duplicate a section, but without the .p1 elements.

You could use the clone()[docs] method to clone the section, the remove()[docs] method to remove what you don't want, and then insert its HTML.

$(document).ready(function() {

    var test = $('#issue_detail_first_row').clone();  // clone it

    test.find('.p1').remove();  // find and remove the class p1 elements

    $('#sett').html( test.html() );  // insert the new content
});

Working example: http://jsfiddle.net/YDZ9U/1/

Only thing is that you'll need to go through and update the IDs in the clone, so that they're not duplicated on the page.

like image 27
user113716 Avatar answered Oct 07 '22 02:10

user113716