Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    18
    Member #
    21022
    hi, I'm new to jquery and javascript/web design in general. I'm messing around with jquery .append to place html code into the div when it's clicked. It works. My question is how do I limit how many times something is appended?

    here's my code:
    Code:
    $("document").ready(function()
            {
                $("#hide") .click(showElem);
            });
        function showElem(evt)
        {
            $("#hide") .append("this has been appended by jquery <br/> html tags seem to be working <br />");
        }
    is this done by just making an "if" statement similiar to php?

  2.  

  3. #2
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    This should work.
    Code:
    $("document").ready(function() {
        $("#hide").click(showElem);
    });
    function showElem(evt) {
        if (!$("#hide").hasClass("appended")) {
            $("#hide").addClass("appended").append("this has been appended by jquery <br/> html tags seem to be working <br />");
        }
    }

  4. #3
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    You could also simplify it using a closure:
    Code:
    $("document").ready(function() {
        $("#hide").click(function (evt) {
            if (!$("#hide").hasClass("appended")) {
                $("#hide").addClass("appended").append("this has been appended by jquery <br/> html tags seem to be working <br />");
            }
        });
    });

  5. #4
    Junior Member
    Join Date
    Jan 2010
    Posts
    18
    Member #
    21022
    thanks! that worked great!


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 01:10 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com