SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Span is a little fibber..

    In the following code:

    span 1 has class "a"
    span 2 has class "b c"

    Clicking span 2 alerts: "b c" - good.
    Then click span 1 to remove class "c" from span 2 - good,
    Click span 2 again and it alerts: "b" - good..

    wait...

    If it no longer has class "c" then why did it still alert? Only class "c" was given instructions to alert..

    Code:
    $(document).ready(function(){
    	$(".a").click(function(){
    		$(".b").removeClass("c");
    	});
    	$(".c").click(function(){
    		alert($(this).attr("class"));
    	});
    });

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,826
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The instruction was to add the alert to everything having class 'c'. There is no instruction there to remove the alert when you remove the class and so the alert is still there even though the class is not.

    You could visualise it like this - imagine each span to be a block of wood, with class 'c' being indicated by a sticker attached to the wood. If we now imagine the alert as being a nail hammered into the wood. Removing the sticker does not remove the nail.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    The instruction was to add the alert to everything having class 'c'. There is no instruction there to remove the alert when you remove the class and so the alert is still there even though the class is not.

    You could visualise it like this - imagine each span to be a block of wood, with class 'c' being indicated by a sticker attached to the wood. If we now imagine the alert as being a nail hammered into the wood. Removing the sticker does not remove the nail.
    Ok, I see what you're saying. But when there's css attached to class "c", removing class "c" takes the css with it. Why is it any different for an alert? And how do I go about removing the alert function?


Bookmarks

Posting Permissions

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