javascriptfunction

Pass function argument as data-toggle attribute?


I would like to write a function and call the function multiple times in order to pass a different data-toggle attribute every time as the argument of the function but it doesn't seems to work? Is this possible?

Edit something like this:

var argument1 = "something";

function test(param) {
    console.log('[data-toggle="param"]');
}

test(argument1);

Solution

  • In order to use the value of the function parameter param you need to use the variable correctly. Right now you are just treating it as a literal part of the string.

    function test(param) {
        console.log('[data-toggle="' + param + '"]');
    }
    

    By closing the string with a single quote, just like it was started, we can now concatenate the value of the function argument param using the + operator. And then by using another + and starting a new string literal, with another single quote, we can continuing building the string.

    Example: https://jsfiddle.net/wsyr5d9s/