0

How do you make a Javascript value have the same value as another variable. I have a variable called "buttonId", and another called "toWin", I want to make it so that buttonId's value doesn't change, but, toWin changes to have the same value as buttonId. something like:

toWin=buttonId;

Can anyone help me? No jQuery, please.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
CPC
  • 163
  • 5
  • 17
  • Your code seems correct, although you should use `var` when declaring new variables. Are those objects or variables? – Sergio Feb 03 '14 at 19:46
  • Yes, I checked the spelling on both variables – CPC Feb 03 '14 at 19:47
  • 1
    What's the *actual* problem? The assignment operator is how to do this. Are you instead having an issue with changing one object and the otherone *also* changes, e.g., two references to the same object? – Dave Newton Feb 03 '14 at 19:47
  • @CPC Sergio asked if they were objects or variables, not if they were spelled correctly. – Dave Newton Feb 03 '14 at 19:47
  • possible duplicate of [Most elegant way to clone a JavaScript object](http://stackoverflow.com/questions/728360/most-elegant-way-to-clone-a-javascript-object) – Sergio Feb 03 '14 at 19:49
  • You mean whenever buttonId changes, toWin automatically changes also?? Or do you actually mean an HTML buttons's value?? – epascarello Feb 03 '14 at 19:49
  • @CPC if these are objects, changing one will change the other. You might want to clone/copy them instead. – Sergio Feb 03 '14 at 19:50
  • I'm kind of new at programming, and I was a little confused at what the definition of an object is, but I googled it, and realized that it actually isn't an object. – CPC Feb 03 '14 at 19:54
  • I figured out what is wrong with it, I had a different problem, this wasn't the problem at all, so I don't really need help anymore, but thanks – CPC Feb 03 '14 at 19:56
  • @CPC please post the solution as an answer and accept your own answer so that future visitors can get help as well. – Chris Bier Feb 03 '14 at 20:04

3 Answers3

1

Just like you have it, but if you're declaring it for the first time, use the var keyword.

var toWin = buttonId;

If buttonId is an object, you may want to clone the object before setting toWin to the value of buttonId to avoid changing the value of the original object (buttonId) when and if you change toWin.

Chris Bier
  • 14,183
  • 17
  • 67
  • 103
  • 1
    Hmm, I was just debugging something, and thought that that might not work, so I just checked on here if it does, so I guess I have a different problem. – CPC Feb 03 '14 at 19:50
1

You forgot to mention var while declaring the second variable. you should do it like this:

var something = '1'; 
var anotherthing = something;

alert(anotherthing);

The output will be 1.

  • I was just debugging something, and thought that that might not work, so I just checked on here if it does, so I guess I have a different problem. – CPC Feb 03 '14 at 19:50
0

This works:

toWin=buttonId;

I was debugging my Javascript, and thought that this might be the problem, so I just checked on here if it works, but now I know that my problem is completely different.

CPC
  • 163
  • 5
  • 17