telexper
telexper

Reputation: 2441

remove a div based on class and id

i'm trying to remove a div based on the id and class but when executing i can only remove it by using an individual value from either the id or class but not both.

EX:

<div id="69" class="form-loader">
    test 69
</div>
<div id="70" class="form-loader">
    test 70
</div>

<input type="text" value="69" id="loader-form-val"/>
<button class="form-delete-con">click</button>

$('.form-delete-con').live("click",function() { 
    var del = $('#loader-form-val').val();
    $('#'+del+' .form-loader').remove();
});

Upvotes: 0

Views: 1442

Answers (2)

Adrift
Adrift

Reputation: 59769

It's the extra white space in the string containing the class name, .form-loader

$('.form-delete-con').live("click",function() { 
    var del = $('#loader-form-val').val();
    $( '#' + del + '.form-loader').remove();
});              // ^ you had ' .form-loader'

http://jsfiddle.net/r3vMW/1/

Upvotes: 1

Roko C. Buljan
Roko C. Buljan

Reputation: 206008

IDs are unique therefore you don't need to bother with any class things

Also from jQuery v.1.7 the .live() method is deprecated and it's substituted with .on()

This will do the job:

$(document).on("click", '.form-delete-con', function() { 
    var del = $('#loader-form-val').val();
    $("#"+ del).remove();
});

Upvotes: 0

Related Questions