0

Hello guys i've got the following problem: How can i set, that the input will be added to the variable? The function Button_Hello_World will be used often and the string input is going to change ten times with different strings. The function is going to be reused thats why different strings will be implied later

var pointsString_To_Add=0;

onclick="javascript:Button_Hello_World(String_To_Add,5)"

function Button_Hello_World(string,variable)
{

       points+string = variable;
}

And now pointsString_To_Add should be 5

3
  • 1
    points+string = points; is not valid syntax! Commented Jan 10, 2017 at 7:10
  • You don't say ;-) Commented Jan 10, 2017 at 7:12
  • Don't write event handlers as a string. It makes it easy to miss syntax issues, such as how you wrote String_To_Add without any quotes around it. Instead, assign a function to onclick. Commented Jan 10, 2017 at 7:16

5 Answers 5

1

Something like

pointsString_To_Add += points;

This is short hand for

pointsString_To_Add = pointsString_To_Add + points;

But I have no idea what your variable string was intended for.

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

Comments

0

Simply assign value to variable:

var pointsString_To_Add = 0;
var String_To_Add = '';

Button_calculate_(String_To_Add, 5)

function Button_calculate_(string, points) {

  pointsString_To_Add = points + string;

  alert('pointsString_To_Add = '+pointsString_To_Add);
}

Comments

0

i think you mean somthing like this

function Button_Hello_World(string, points) {

    window[string] = points;

}

this will find value you passed and set the points to it

Comments

0
var pointsString_To_Add=0, string_TO_Add = "";

onclick="javascript:Button_Hello_World(string_TO_Add ,5)"

function Button_Hello_World(string,points)
{

       pointsString_To_Add = string+points;
alert('Result Expecting'+pointsString_To_Add);

}

Comments

0

Seems you are putting a variable in the function's args. Either you concatenate it and place it in the DOM or just put the string value there. Better to go with addEventListener:

var pointsString_To_Add=0;
var String_To_Add = ""; // put the actual value here.
var el = document.querySelector('theElement/tag/#id/.class');
el.addEventListener('click', function(ev){ // Add a click event here
    Button_Hello_World(str,points); // call the function
});

function Button_Hello_World(str,points){ // change to str as string is reserved
    points += str || 0; // if str is undefined then 0 will be added.
    console.log(points);
}

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.