Concatenate problem using jQuery

Asked

Viewed 27 times

0

I have the following example that I cannot do.

I have a div:

<div class="dados" data-attribute="2" onclick="bloquearSacado(1,2);">
       <p>Teste</p>
        </div>

And I have the following code:

status = 3;
codigo = 8;
$('.dados[data-attribute='+ id + ']').attr('onclick',(status+, id));

When I run this code it adds the values 1+8 but I need it to be like this:

 onclick="bloquearSacado(3,8);"

What is the right way to concatenate in this situation so that it is equal to the result above?

PS: the ( and , are fundamental.

Thank you very much.

  • Present the code, it’s easier to show the problem than trying to explain and we’ll still have a [MCVE]

  • But I presented the code: it is a div <div class="data" data-attribute="2" onclick="blockSacado(1,2);"> <p>Test</p> </div>

  • That I execute the code $('.data[data-attribute='+ id + ']'). attr('onclick',(status+, id)); with variable status = 2 and id = 9 and it adds the two values in onclick and not CONCATENATE, understood?

  • Where the variable codigo get into it?

  • I think you put the wrong code. That would be it: $('.dados[data-attribute='+ id + ']').attr('onclick','bloquearSacado('+status+','+codigo+')');

  • And it doesn’t make much sense for you to have an onClick event and want to bind another one through the selector... present the code

Show 1 more comment

1 answer

5

Your problem starts in syntax error:

(status+, id)

That comma after the + causes error because it has no way to add or concatenate two variables with a comma in the middle.

If you want to change the element attribute with the two variables status and codigo, the correct would be to put also the function name and change the values inside the parentheses by concatenating the values outside the quotation marks of the method:

$('.dados[data-attribute='+ id + ']').attr('onclick','bloquearSacado('+status+','+codigo+')');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.