0

the problem is i want to shorten my code by calling a variable using other variable's value

long working version:

var russia = new Array('15')
var switzerland = new Array('5')

$('.country').mouseover(function(){
    switch(this.id){
        case 'russia':
            active_country_lift(this.id,russia[0])
        break
        case 'switzerland':
            active_country_lift(this.id,switzerland[0])
        break           
    }
})

it will get the id of mouseovered then check if it matched one of the variable by using switch

what i want to obtain is something like this:

var russia = new Array('15')
var switzerland = new Array('5')

$('.country').mouseover(function(){
    active_country_lift(this.id,this.id[0])     
})

of course the above code wouldn't work but is there a workaround for this?

UPDATE: Arun's answer worked and ill accept it soon and as for the comments requesting for the full code, here's a chunk of it after i applied Arun's

var countries = {
    russia: ['-15px'],
    switzerland: ['-5px']
}

$('.country_inactive').mouseover(function(){
    active_country_lift(this.id, countries[this.id][0])
})

function active_country_lift(country, country_top){
    if(!$('#'+country+'_active').hasClass('active')){
        $('#'+country+'_active').stop().fadeIn(100).animate({
            'top' : country_top
        }, 200)
        $('#'+country).stop().fadeOut(100)
    }
}

it will be used for a world map, feel free to make any suggestions for making it more dynamic

3
  • 3
    Pro tip: never use new Array in JS unless you are declaring the length of the array. Just initialize the array using []. Commented Apr 22, 2016 at 3:36
  • Can you add complete code of what you're trying to achieve, this can probably achieved using CSS only. Commented Apr 22, 2016 at 3:40
  • Possible duplicate of Get global variable dynamically by name string in JavaScript Commented Apr 22, 2016 at 3:59

2 Answers 2

4

You can store the country info in an object like a key value pair, then use bracket notation to access it dynamically

var countries = {
  russia: new Array('-15px'),
  switzerland: new Array('-5px')
}

$('.country').mouseover(function() {
  active_country_lift(this.id, countries[this.id][0])
})

If you don't have multiple values then

var countries = {
  russia: '-15px',
  switzerland: '-5px'
}

$('.country').mouseover(function() {
  active_country_lift(this.id, countries[this.id])
})
Sign up to request clarification or add additional context in comments.

6 Comments

new Array('-15px') ==> '-15px'. Why use array.
@Tushar it actually gives ['-15px']
@Tushar there could be multiple values, not sure about that
If russia and switzerland are globals, you could even just do window[this.id] instead of creating a wrapping object, but creating a wrapping object is better because global variables are bad practice.
@4castle yes, you can. But using object is better practice instead of globals
|
0

try using eval() function

var russia = new Array('-15px')
var switzerland = new Array('-5px')

$('.country').mouseover(function(){
   active_country_lift(this.id,eval(this.id)[0])     
})

1 Comment

Most people pronounce eval as evil because it is evil and inefficient. This is a valid answer though.

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.