Smoking monkey
Smoking monkey

Reputation: 323

How to remove a div with particular id containing particular text?

if (!$(this).is(":checked")) {
    $.each(function(){
        if ($('#result').has("Apartment")) {
            $('#result').remove();
        }
    })
}

I want that if I uncheck a checkbox then the div having id="result" should be deleted which contains the text "Apartment". Currently this logic is not working for me.

Upvotes: 0

Views: 71

Answers (4)

collab-with-tushar-raj
collab-with-tushar-raj

Reputation: 1287

Try this http://jsfiddle.net/Tushar490/bg82uw8s/

var IsCheck;
var txt = $("#result").text();
$("#chk").change(function () {
IsCheck = $("#chk").is(":checked");
if (!IsCheck) {
    if (txt === 'Apartment') {
        $("#result").remove();
    }
  }
});

Upvotes: 0

VPK
VPK

Reputation: 3090

Try this,

$('#checkboxId').change(function(){
    if(!$(this).is(':checked')) {
        $('#result:contains(Apartment)').remove();
    }
});

DEMO

Upvotes: 2

Rob Sedgwick
Rob Sedgwick

Reputation: 5226

particular id containing particular text?

if( $('elementselector').attr('id').indexOf('Apartment')>-1) { ..... }

unless there is some new JQ method out there

( note - elements must have an ID, or check has ID first etc .. )

Upvotes: 0

user733421
user733421

Reputation: 497

or

 $('#checkboxId').change(function(){
        if(!$(this).is(':checked')) {
            if($('#result').text().indexOf("mystring")>-1) {
                $('#result').remove();
            }

        }
    });

Upvotes: 0

Related Questions