-1

I asked this earlier, but did a dreadful job describing it. I am looking to make a link that you click and it makes a variable true at random, starting a chain of animations/event. Is this possible to do with jQuery? Thanks for your help.

Edit:

Best way to describe it would be:

var random1 === false 
var random2 === false 
var random3 === false 

$("a#random-btn").click(function(event){ 
"var random1, random2, or random3" === true;
}); 

if (random1 === true){ 
$("a#1 span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100')
});

if (random2 === true){ 
$("a#2 span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100')
});

if (random3 === true){ 
$("a#3 span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100')
});
Shopmelk
  • 15
  • 4
  • variable is in javascript or in scripting language?? – xkeshav Oct 24 '11 at 04:50
  • Please do not post duplicate questions. http://stackoverflow.com/questions/7869813/making-1-of-6-variables-true-randomly-in-jquery – Sparky Oct 24 '11 at 04:55
  • could you describe it beautifull again, because I don't understand it... ar you looking for a timeout? – beardhatcode Oct 24 '11 at 04:56
  • Could you describe what you want to achieve in "general" terms as that would allow people to understand the problem rather than a particular solution you are asking about – Ankur Oct 24 '11 at 05:01
  • @Ankur For some reason it's rather difficult to explain. Let say when you click the "random" button, it executes var 1, 2 or 3. The Vars have the own if statements to the proper animation I want to achieve. – Shopmelk Oct 24 '11 at 05:05
  • @Sparky672 My apologies. I am honestly lost, but this is a key section of the site. I haven't been able to code a solution, and that rendered no working solutions. I thought if I explained it better, it would work out better. – Shopmelk Oct 24 '11 at 05:08
  • The protocol here would be to edit your original question to make it more clear. This is the most fair & courteous way to treat all the people who already tried to help you in your previous posting. – Sparky Oct 24 '11 at 05:26
  • @Sparky672 I see. Shant happen again. – Shopmelk Oct 24 '11 at 05:36

3 Answers3

1

NOTE: my answer does not let you assign variables as true randomly. I guess you are trying to assign the variable true and use it(them) to do some other stuff, right? Why not use an int, change it and use switch statement for the logic?

var branch = -1;
$("a#random-btn").click(function(event){ 
    branch = Math.ceil(Math.random() * 3);

    switch(branch){
        case 1:
            $("a#1 span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100')
            break;
        case 2:
            $("a#2 span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100')
            break;
        case 3:
            $("a#3 span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100')
            break;
    }
}); 

or more compact

var branch = -1;
$("a#random-btn").click(function(event){ 
    branch = Math.ceil(Math.random() * 3);

    $("a#" + branch + " span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100')
}); 
Liangliang Zheng
  • 1,763
  • 11
  • 16
0

What do you mean by making a variable true at random? and also what do you mean by starting a chain of animations/event? I really dont understand the question.

But if you wanted to make a variable change or something during click, just do this.

jQuery('a').click(function(){
var myVariableFalse = true;
});
PinoyStackOverflower
  • 5,214
  • 18
  • 63
  • 126
  • It can't really be a proper answer if you don't understand the question. – Sparky Oct 24 '11 at 04:56
  • As in `var random1 === false var random2 === false var random3 === false $("a#random-btn").click(function(event){ var random1, random2, or random3 === true; }); if (random1 === true){ $("a#1 span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100'); } ` if – Shopmelk Oct 24 '11 at 05:01
0

It's really kludgey, but this works.

<script>
    var var1 = false;
    var var2 = false;
    var othervar = false;
    var variables = ["var1","var2","othervar"];
</script>
<a href="Javascript:eval(variables[Math.floor(Math.random()*variables.length)] + ' = true;');">link</a>

My suggestion would be to have all the variables you want to change be in an object which is then easily referenced by a string.

<script>
    Object.size = function(obj) {
        var size = 0, key;
        for (key in obj) {
            if (obj.hasOwnProperty(key)) size++;
        }
        return size;
    };
    var variables = {"var1":false,"var2":false,"othervar":false};
</script>
<a href="Javascript:variables[Math.floor(Math.random()*Object.size(variables))] = true;">link</a>

Object.size taken from here.

Community
  • 1
  • 1
Nick Q.
  • 3,947
  • 2
  • 23
  • 37