1

Hi guys so i am trying to change the value of a javascript variable depending on what button is selected. For example

<button id="1" onclick="changeVal('val1')">b1</button>
<button id="2" onclick="changeVal('val2')">b2</button>

<script>
var value;
function changeVal(choice) {
  if (choice == 'val1') {
    value = 'number value 1'
  } else if (choice == 'val2'){
    value ='number value 2'
  }
*etc for more buttons*
}
</script>

The variable value will then be used later on. Can't really wrap my head around the problem, im either doing it completely wrong or so close but can't quite get there.

edit: So still having trouble, basically i want to return that choice value so it can then be called in another function e.g.

<script>
var value... 
function changeval... //Return choice value out

function useValue() {
    alert(choice) //Use choice value here
}

note ignore syntax wrote quickly.

4
  • What is the problem here exactly? Is the value variable not assigned to the value that you want? Commented Aug 21, 2016 at 16:19
  • @biseibutsu so im using the variable later on in another function, but it doesn't seem to run, is my code above correct? If so it means my issue is with my other function, but that is GIS openlayers related so will have to post there. Commented Aug 21, 2016 at 16:27
  • From what you have posted this code should work, but I cannot say for sure since I dont know where it doesn't run. Commented Aug 21, 2016 at 16:28
  • @biseibutsu ok thanks, i have left my computer so can't post specific openlayers script. But will give it a try tomorrow morning and update you. Commented Aug 21, 2016 at 16:35

2 Answers 2

1

The above code runs perfectly in a web browser. The problem must be with the concept of variable scope. Or in the function you are using the value variable.

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

1 Comment

sorry for the late reply, so im still having trouble, basically i want to use that variable in another function further down, but the way javascript runs means the 'choice' just keeps the undefined value even once the button has been pressed. Any way around this?
1

Please consider assigning the "choice" variable to a global variable, so that it can be accessed outside the function. Sample snippet is as follows:

<button id="1" onclick="changeVal('val1')">b1</button>
<button id="2" onclick="changeVal('val2')">b2</button>
<button id="3" onclick="display()">Display Value</button>
<script>
    var value = "";
    var globalChoice ="";
    function changeVal(choice) {
        if (choice == 'val1') {
            value = 'number value 1';
        } else if (choice == 'val2'){
            value ='number value 2';
        }
       globalChoice = choice;
    }
    function display(){
        alert(globalChoice);
    }

</script>

1 Comment

thanks for the quick reply, will implement and see how i go

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.