Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Location
    Toronto, ON
    Posts
    102
    Thanks
    12
    Thanked 1 Time in 1 Post

    Hide a div using $(this).attr('id') not working

    Hi guys.

    I have a bunch of divs that have id's in the following format: <div id="cid183">. The id's are dynamically assigned so one might be cid183, cid195 etc and they are all unique values.

    I have a delete button in each div. When a user clicks this delete button i run some ajax which successfully removes some info from the db, but then I want to simply fade out the div. The delete button has an id that contains some other irrelvant information and then the id of the div to be removed in this format: <span class="delete" id="u544-183">delete</span> - the value of 183 corresponds to the div name cid183.

    The ajax is working as the information is being removed from my db properly, but the div won't disappear. Here is the relevant code:

    Code:
    <script>
    $('.delete').click(function() {
    	$.ajax({
    		type: 'get',
    		url: '../delete.php',
    		data: 'ajax=1&delete=' + $(this).attr('id'),
    		success: function() {
    			var myid = $(this).attr('id');
    			var startsub =  myid.indexOf('-')+1;
    			var toremove = myid.substring(startsub);
    			$("#cid"+toremove).fadeOut();
    		}
    	});	        
    });
    </script>
    
    <div id="cid183">
        This is the content of the div
        <span class="delete" id="u544-183">delete</span>
    </div>
    I'm guessing it's messing up in my indexOf, substring stuff....I don't really know.

    Sorry for the noob question!!!
    Last edited by inchecksolution; 01-28-2012 at 11:22 PM.

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,479
    Thanks
    6
    Thanked 981 Times in 954 Posts
    You don’t need all that crap. Just call
    Code:
    $(this).parent().fadeOut();
    Or, ideally, you would actually remove the element:
    Code:
    $(this).parent().fadeOut(function() {
      $(this).remove();
    });


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •