Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define global variable in JQuery

I'm not sure if I'm explaining this right, but here it goes ...

I have a function that works in JQuery to assign the selected dropdown value to a variable and then pass the variable to a different part of the HTML when a confirmation button is clicked.

Here's a stripped down version of the HTML

<p id="t1"></p> 
<select id="selectLevel">
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
   <option value="4">4</option>
   <option value="5">5</option>
   <option value="6">6</option>
   <option value="7">7</option>
   <option value="8">8</option>
   <option value="9">9</option>
   <option value="10">10</option>
   <option value="11">11</option>
   <option value="12">12</option>
   <option value="13">13</option>
   <option value="14">14</option>
   <option value="15">15</option>
   <option value="16">16</option>
   <option value="17">17</option>
   <option value="18">18</option>
   <option value="19">19</option>
   <option value="20">20</option>
</select>
<span class="btn" id="confirmLevel">Confirm</span>

And here's the JQuery I used.

$(document).ready(function() {
    $('#confirmLevel').click(function() {
        var PClevel = $("#selectLevel option:selected").text();
        $('#t1').append('Level ' + PClevel); 
    });
});

The problem is, if I later try to call the PCLevel variable for other functions, nothing happens. What am I missing here? Or would it be better to skip JQuery altogether and just use Javascript to do the same thing?

like image 717
Alicia Avatar asked May 10 '16 02:05

Alicia


People also ask

How do I declare a global variable in jQuery?

Simply declare the variable on the global scope. var PClevel = ''; $(document). ready(function() { $('#confirmLevel'). click(function() { PClevel = $("#selectLevel option:selected").

How do you define a global variable?

In computer programming, a global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program, unless shadowed. The set of all global variables is known as the global environment or global state.

How do you define a global variable in JavaScript?

Global Variables are the variables that can be accessed from anywhere in the program. These are the variables that are declared in the main body of the source code and outside all the functions. These variables are available to every function to access. Var keyword is used to declare variables globally.

How do you declare a variable in jQuery?

Declare a variable using the var keyword. Initialize using the = symbol. Example: var hText = "This is just some text." - Click on the heading to see the text displayed in the <p> elements.


2 Answers

The problem is that PClevel is scoped inside the click handler. It cannot be accessed outside. In order for code outside to see PClevel, declare it outside and have the click handler just modify it.

$(document).ready(function() {
    var PClevel; // Code inside the ready handler can see it.

    $('#confirmLevel').click(function() {
        PClevel = $("#selectLevel option:selected").text();
        $('#t1').append('Level ' + PClevel); 
    });
});

// or

var PClevel; // Now it's a global. Everyone can see it.
$(document).ready(function() {
    $('#confirmLevel').click(function() {
        PClevel = $("#selectLevel option:selected").text();
        $('#t1').append('Level ' + PClevel); 
    });
});
like image 94
Joseph Avatar answered Sep 21 '22 11:09

Joseph


Simply declare the variable on the global scope.

var PClevel = '';
$(document).ready(function() {
    $('#confirmLevel').click(function() {
        PClevel = $("#selectLevel option:selected").text();
        $('#t1').append('Level ' + PClevel); 
    });
});

It would be worth reading up on javascript scopes as they are relevant whether you are using jQuery or not.

http://www.sitepoint.com/demystifying-javascript-variable-scope-hoisting/

like image 32
DGS Avatar answered Sep 19 '22 11:09

DGS