How to get variable value outside a function IN jquery

sam

I have a variable i assign value to it inside a function now i want to get this value outside of this variable but i get undefined, below is my code. I have select2 on my page whcih i want to populate with some data depend on what users choices are, i allow a user to choose a radio button and i get the value of the radio button to determine which data to display in the select menu. Now after my coding i could alert var size_data within the onclick event function but couldn't get this value to the select2 where i need it, any help on this will be appreciated

Note var size_data is a global variable and the variable that holds different sizes are also global variable

see my DEMO ON FIDDLE

var uk_size = [{ id: '8', text: '8' },
                { id: '10', text: '10' }, 
                { id: '12', text: '12' },
            ];
var us_size = [{ id: '4', text: '4' },
                { id: '6', text: '6' }, 
                { id: '8', text: '8' },
                { id: '10', text: '10' }, 
            ];
var eu_size = [{ id: '34', text: '34' },
                { id: '36', text: '36' }, 
                { id: '38', text: '38' },
            ];
var ng_size = [{ id: 'XS', text: 'XS' },
                { id: 'S', text: 'S' }, 
                { id: 'M', text: 'M' },
            ];
var size_data = ng_size; //default value;
//alert(ng_size);
   $("#size-style input:radio").on('click',function() {
              //alert(ng_size);

                  var size_style ="";
                size_style = $(this).val();
                if (size_style === "ng-size"){
                      size_data = ng_size;  
                }else if(size_style === "uk-size"){
                      size_data = uk_size; 
                }else if(size_style === "us-size"){
                      size_data = us_size;
                }else{
                    size_data = eu_size;
                }
                alert(size_data);//can alert jquery object here
    });



$('select.size-select').select2({
        name: 'size',
        value: 'S',
        data: size_data,
        tags: "true",
        placeholder: "Select size",
        allowClear: true
    });
vijayP

You can initialize the select2 with default value first and then in radio button click event you can modify its data like below:

https://fiddle.jshell.net/nnaL44ur/4/

$("#size-style input:radio").on('click', function() {
  //alert(ng_size);

  var size_style = "";
  size_style = $(this).val();
  if (size_style === "ng-size") {
    size_data = ng_size;
  } else if (size_style === "uk-size") {
    size_data = uk_size;
  } else if (size_style === "us-size") {
    size_data = us_size;
  } else {
    size_data = eu_size;
  }
  alert(size_data); //can alert jquery object here

  var $select = $('select.size-select'); //grabbing the select element

  //fetching the previously set options at the first time initialization of select2 widget
  var options = $select.data('select2').options.options;

  $select.html(''); //clearing the previous dropdown options
  // add new items to the options
  options.data = size_data;
  $select.select2(options); //updating the select2 widget with newer set of option data

});

hope it will help you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get variable value outside angular function

From Dev

how to get value outside jquery click function

From Dev

JQuery change variable value outside the function

From Dev

Get the variable outside foreach function? using jquery

From Dev

How to assign value to variable outside the function in $on AngularJS?

From Dev

How call variable outside function .each with Jquery?

From Dev

jQuery: How to return a value outside a function that contains the 'this'?

From Dev

sails: how to get a value outside of the function

From Dev

Jquery variable value doesn't work outside the function

From Dev

Get the value of a variable and array outside a self-invoking function

From Dev

Variable not retaining value outside of function

From Dev

Retrieve value of variable outside function

From Dev

accessing variable value outside of function

From Dev

jQuery use variable outside function

From Dev

Get variable value outside of Promise

From Dev

How to capture current value of an outside variable when function is defined

From Dev

how to get variable out of function(){ $.get() return variable; } function ? in jquery?

From Dev

bash function - Get function stdout value inside a variable and modify variables outside the function

From Dev

Setting a value in a function as a variable outside the function (Swift)

From Dev

How can I get clientHeight value outside backbone render function?

From Dev

How I can get the value of "j" for use outside the function?

From Dev

How can I get clientHeight value outside backbone render function?

From Dev

How to get value outside onclick function after redirection to other page

From Dev

How to get variables from the outside, inside a function in jQuery?

From Dev

How to Manipulate Variable Value inside a Function When The Variable was Declared Outside the Function?

From Dev

Using variable outside of jquery.click function?

From Dev

Access jQuery variable outside its function

From Dev

jQuery plugin - using variable outside function

From Dev

Cannot access variable outside function jquery

Related Related

  1. 1

    How to get variable value outside angular function

  2. 2

    how to get value outside jquery click function

  3. 3

    JQuery change variable value outside the function

  4. 4

    Get the variable outside foreach function? using jquery

  5. 5

    How to assign value to variable outside the function in $on AngularJS?

  6. 6

    How call variable outside function .each with Jquery?

  7. 7

    jQuery: How to return a value outside a function that contains the 'this'?

  8. 8

    sails: how to get a value outside of the function

  9. 9

    Jquery variable value doesn't work outside the function

  10. 10

    Get the value of a variable and array outside a self-invoking function

  11. 11

    Variable not retaining value outside of function

  12. 12

    Retrieve value of variable outside function

  13. 13

    accessing variable value outside of function

  14. 14

    jQuery use variable outside function

  15. 15

    Get variable value outside of Promise

  16. 16

    How to capture current value of an outside variable when function is defined

  17. 17

    how to get variable out of function(){ $.get() return variable; } function ? in jquery?

  18. 18

    bash function - Get function stdout value inside a variable and modify variables outside the function

  19. 19

    Setting a value in a function as a variable outside the function (Swift)

  20. 20

    How can I get clientHeight value outside backbone render function?

  21. 21

    How I can get the value of "j" for use outside the function?

  22. 22

    How can I get clientHeight value outside backbone render function?

  23. 23

    How to get value outside onclick function after redirection to other page

  24. 24

    How to get variables from the outside, inside a function in jQuery?

  25. 25

    How to Manipulate Variable Value inside a Function When The Variable was Declared Outside the Function?

  26. 26

    Using variable outside of jquery.click function?

  27. 27

    Access jQuery variable outside its function

  28. 28

    jQuery plugin - using variable outside function

  29. 29

    Cannot access variable outside function jquery

HotTag

Archive