views:

43

answers:

3

When I click on a particular div, that div should fade out, simple, but when I click on one of the divs it deletes the div on top of the stack, i.e. when I click #sel6 it removes sel5

HTML code

<div id="selc_d" class="selc" style="position:absolute; left:15px; top:200px; width:260px;">

<div id="sel5" class="sel">something</div>
<div id="sel6" class="sel">something</div>
<div id="sel7" class="sel">something</div>

</div>

jQuery code sel_id, sel_1 are variables

$('.selc_d').bind('click',function(){ 
  var sel_id = $('.sel').attr('id');
  alert(sel_id);

  $('#'+sel_id).fadeOut('slow');
  $('#'+sel_id).remove();
  $('.search_box').append(sel_1);
});
+2  A: 
$('.sel').bind('click',function(){
    var sel_id = this.id; // replace this line using this.id or $(this).attr('id');
    alert(sel_id);
    $('#'+sel_id).fadeOut('slow');
    $('#'+sel_id).remove();
    $('.search_box').append(sel_1);
});

Note the use of this... this will contain the element that is clicked.

What you were doing was

var sel_id = $('.sel').attr('id'); 

Which will always select the first div with class sel in this case the div with id sel5

##WHAT YOU WANT##

<div id="selc_d" class="selc" style="position:absolute; left:15px; top:200px; width:260px;">

    <div id="sel5" class="sel">something</div>
    <div id="sel6" class="sel">something</div>
    <div id="sel7" class="sel">something</div>

</div>

$('.sel').bind('click',function(){
    var sel_id = $(this).attr('id');
    alert(sel_id);
    $('#'+sel_id).fadeOut('slow');
    $('#'+sel_id).remove();
    $('.search_box').append(sel_1);
});
Lizard
my mistake.Please check the jquery code againquestion edited
Jean
It still looks incorrect, `var sel_id = $('.sel').attr('id');` that code will never grab the correct id for the element you are clicking, only ever the first.
Lizard
@lizard i changed the question as'$('.selc_d').bind('click',function(){ '
Jean
I know, the incorrect line is `var sel_id = $('.sel').attr('id');` please change that to this.id or $(this).attr('id')
Lizard
Strangely, the click is not happening on .sel. I know it should but not happening
Jean
If you have `$('.selc_d').bind('click',function(){ ` Then NO click will work... as you dont have an element with that class
Lizard
I did $('.selc').bind('click') but after reading your code, I changed to $('.sel'), but does not work.
Jean
Try var sel_id = this.id; Let me know if this works
Lizard
@lizardthe issue is the click function is not triggered with .sel
Jean
It works fine for me.. Check out http://londoncigarettecard.co.uk/test2.php
Lizard
+2  A: 

it looks like, you are trying something like this

$('.sel').bind('click', function(){
    $this = $(this);
    $this.fadeOut('slow', function(){$this.remove();});
    $('.search_box').append(this.id);
});
jAndy
edited the question to $('.selc_d').bind('click',function(){
Jean
A: 

That's because you are saying $('.sel')

You need to grab the one that is clicked, not any one that has that class. To make that work as you want, do this

$('.sel').click( function() {
    var sel_id = $(this).attr('id');

    $(this).fadeOut('slow');

    // Wait for it to fade out before you remove it
    setTimeout( function() {
        $('#' + sel_id).remove();
    }, 1000 );


    $('.search_box:first').append(sel_id);
});
Kerry
if you want to wait, use callback for fadeOut.. and not setTimeout...
Reigel
that is really bad karma
jAndy
@ Reigel callback doesn't always work. setTimeout does.
Kerry