Get JavaScript dynamic variable

Discussion in 'JavaScript' started by Hefaistos, Apr 13, 2013.

  1. #1
    Hi. My knowledge of javascript are limited and I bumped into a simple thing today , but it seems that I can get it done.

    I've got a javascript file , and to the top of this I have something like:

    
    var options = {
        "background":{
            "colour":"red",
            "pattern":"lines"
        },
        "text":{
            "style":"bold",
            "arrange":"middle"
        }
    };
    
    Code (markup):
    So , what I'm trying to do is to take each key's value: Eg. options -> background -> colour and I should get "red".

    It works well if I use with an alert like:

    alert(options.background['colour']);
    Code (markup):
    but how would I get that value to this (eg. flexslider's atributes ) :

    
        $('#blog-slider').flexslider({
                slideshow: true,
                background: "red" //  where 'red' will be the value from options.background['colour']
        });
    
    Code (markup):
    (There's no attribute for flexslider "background" , but is just an example)
    Probably it's a simple thing , but a little help will be great.
     
    Hefaistos, Apr 13, 2013 IP
  2. lerell

    lerell Greenhorn

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #2
    $('#blog-slider').flexslider({
                slideshow: true,
                background: options.background.colour
    });
    Code (markup):
    ... or am I something missing?

    PS: calling the
     var colour = options.background.colour; 
    Code (markup):
    is the same as

     var colour = options.background['colour']; 
    Code (markup):
    there is no reason to write code like in second example
     
    Last edited: Apr 15, 2013
    lerell, Apr 15, 2013 IP
  3. lerell

    lerell Greenhorn

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    one more thing. To play with javascript press: Ctrl+Shift+J
    javascript console will pop up (in firefox and chrome) and you can play with javascript and learn how things works.
    keep it up, don't give up ;) God Bless
     
    lerell, Apr 15, 2013 IP
  4. udores

    udores Member

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #4
    Pls adjust your options object to some thing like this

    
    var options = { 
       background: { 
          colour: "red",
          pattern: "lines" 
          text: { 
             style: "bold",   
             arrange: "middle"
          }
    
    Code (markup):
    to give your $('#blog-slider') jquery object the red options property, do it like this

    
    $('#blog-slider').prop(flexslider).prop(background) = options.background.color;
    
    Code (markup):
    NOTE: you use the prop() function to access jquery object properties.

    hope this helps:)
     
    udores, Apr 16, 2013 IP
  5. Hefaistos

    Hefaistos Active Member

    Messages:
    194
    Likes Received:
    14
    Best Answers:
    9
    Trophy Points:
    63
    Digital Goods:
    1
    #5
    I can't get options object like this, because it's generated automatically by a WordPress function (wp_localize_script) inside the <head></head> tags and I can't edit this.


    Thanks both for your answers , I've managed to do this. The problem seemed to be in a php array that was generating this object.
     
    Last edited: Apr 18, 2013
    Hefaistos, Apr 18, 2013 IP