jQuery function parameters not working properly

javascript function parameter default value
javascript function arguments
javascript optional parameters
pass parameter to javascript function onclick
how to pass parameter in javascript function from html
es6 function arguments
how to pass an array as an argument to a function in javascript
javascript pass variable number of arguments to function

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?

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

setInterval with a function PARAMETER of global variable not working, https://jsfiddle.net/jsfiddle135/xpvt214o/956977/ Symptom: setInterval works fine with a global variable WITHOUT ANY parameter. But with. ive a function that does calculations on a table using jquery when any input field is updated. It works fine but i need to be able to call it update a specific row when i load in data. I cant seem to get it working properly either it updates when the an input field changes and it wont work when i call it to update a specific row.

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);
        }
    }

jQuery API Documentation, Insert content, specified by the parameter, after each element in the set of matched elements. Attach a function to be executed whenever an Ajax request completes successfully. Determine whether or not the list has any callbacks attached. Stop the currently-running animation, remove all queued animations, and  The parameters, in a function call, are the function's arguments. JavaScript arguments are passed by value: The function only gets to know the values, not the argument's locations. If a function changes an argument's value, it does not change the parameter's original value. Changes to arguments are not visible (reflected) outside the function.

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")

JavaScript Function Parameters, A JavaScript function does not perform any checking on parameter values (​arguments). Function Parameters and Arguments. Earlier in this tutorial, you learned  Different ways to declare a JavaScript/jQuery functions and which one you should use from get/set, $.fn, jQuery.fn, jQuery.prototype, jQuery.fn.extend etc.

Understanding Default Parameters in Javascript, While processing left to right, the compiler is smart enough to not Javascript has two answers to the not present problem, null and undefined . You can also call a function and use the value returned as a default parameter. In FF is working properly But NOT in Chrome. In Chrome the element start in the LEFT, and then move to the right (fail). In FF start in the original position, and then move to the left (ok). Sorry for my bad english.

The Beginner's Guide to JavaScript Default Parameters, This tutorial teaches you how to use JavaScript default parameters for functions and shows you In JavaScript, default function parameters allow you to initialize named parameters with When we declared the date() function, the today() function has not yet evaluated until we called the date() function. All Right Reserved. offset(function(index, coords)) does not properly pass index: bug offset 1.4.2 Description: When offset() is used as a setter, passed a function, it's supposed to pass the index of the current element in the JQ collection as the first parameter to that function. But it seems to always pass 0.

Function.prototype.call(), Parameters. thisArg Optional. The value to use as this when calling func . Caution​: In certain cases, thisArg may not be the actual value seen by  Ajax call with multiple parameter not working. , success: function (msg yes i checked data is sent properly, but still the same issue its not calling the

Comments
  • Change $(this) to $(target)
  • @ArkadiuszG., it doesn't work.
  • 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