0

I have the following script:

<!doctype HTML>
<html lang = "en">
    <head>
        <title>Learn jQuery</title>
        <link rel = "stylesheet" href = "CSS/style.css">
        <script type = "text/javascript" src = "scripts/java.js"></script>
        <script type = "text/javascript" src = "scripts/jquery.js"></script>
    </head>
    <body>
        <div id = "main">Click to Show and Hide <button id = "btnA">+</button></div>
        <div id = "box">Some content.</div>
        <script>
            $(document).ready(function()
            {
                $("#btnA").click(function()
                {
                    $("#box").slideToggle(2000);

                    // code block in question
                    if  (val == "+")
                    {
                        $(this).html("-");
                    }
                    else if  (val == "-")
                    {
                        $(this).html("+");
                    }
                });
            });
        </script>
    </body>
</html>

I wish to transform the code block in question into a re-usable function like this:

function toggleContent(target, contentA, contentB)
{
    var val = $(target).html();
    if  (val == contentA)
    {
        $(this).html(contentB);
    }
    else if (val == contentB)
    {
        $(this).html(contentA);
    }
}

However when I call toggleContent("#btnA", "+", "-");, it doesn't work. Could someone point out my error? TIA.

The suggested changes so far does not work, even the DEMO. Could someone help?

2
  • 2
    Change $(this) to $(target) Commented Nov 30, 2018 at 6:06
  • @ArkadiuszG., it doesn't work. Commented Nov 30, 2018 at 11:13

3 Answers 3

1

Arkadiusz G's comment may give you the answer.

Change

function toggleContent(target, contentA, contentB)
{
    var val = $(target).html();
    if  (val == contentA)
    {
        $(this).html(contentB);
    }
    else if (val == contentB)
    {
        $(this).html(contentA);
    }
}

To

function toggleContent(target, contentA, contentB)
{
    var val = $(target).html();
    if  (val == contentA)
    {
        $(target).html(contentB);
    }
    else if (val == contentB)
    {
        $(target).html(contentA);
    }
}

UPDATE

And also add this

$(function(e){
$('body').on('click','#btnA','',function(){
   toggleContent("#btnA", "+", "-");
});

});

DEMO HERE

Sign up to request clarification or add additional context in comments.

6 Comments

Wow! It works. Could you explain to me why it doesn't work it I just call $("#btnA").click(function(){$("#box").slideToggle(2000); toggleContent("#btnA", "+", "-");}); in the main script?
It does on your demo, friend. But when I put it into my HTML, it doesn't.
may be some other scripts stops it. do you have any error in developer console?
It says this: Uncaught ReferenceError: something is not defined at HTMLButtonElement.<anonymous> (index.html:21) at HTMLButtonElement.dispatch (jquery.js:5183) at HTMLButtonElement.elemData.handle (jquery.js:4991)
i beleive toggleContent function is outside document.ready
|
1

Inside the function "toggleContent", the "this" object is not what you think, try this way:

function toggleContent(target, contentA, contentB)
    {
        var val = $(target).html();
        if  (val == contentA)
        {
            $(target).html(contentB);
        }
        else if (val == contentB)
        {
            $(target).html(contentA);
        }
    }

Comments

0

Hi if you want to convert it to function pass currentObj also to the function.

function toggleContent(currentObj,target, contentA, contentB)
{
    var val = $(target).html();
    if  (val == contentA)
    {
        currentObj.html(contentB);
    }
    else if (val == contentB)
    {
        currentObj.html(contentA);
    }
} 
toggleContent($(this), $("#btnA"), "contentA", "contentB")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.