Making 1 of 6 Variables true randomly in jquery


I am trying to make a one of a series of divs appear randomly with jquery along with it's navigation link(i.e. If services gets pick, the services link will unfade). I have found this code various times in various forms on this forum, and was wondering if and how I could adapt it to what I would want.

var services = $(random1, random2, random3).get() .sort(function(){return Math.round(Math.random());}).slice(0,1) $(services)/*Conditions here*/; var random1 = false; var random2 = false; var random3 = false;

This is a really bad example, I know. I am get lost on it. Any help would be greatly apprecaited, and thanks in advance.

EDIT: I did try to make an easier comparison earlier, but here is what I am actually working on. I tried to adapt the code from @pst.

var v1 = "hello" var v2 = "world" var control = [ function (v) { v1 = v }, function (v) { v2 = v } ] $.each(control, function (i, fn) { fn(false) }) $("a#random-btn").click(function(event){ event.preventDefault(); var trueIdx = Math.floor(control.length * Math.random()) props[trueIdx](true) if (v1 === true){ $("div#small-obstacles-contain a#1 span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100'); $("div#small-obstacles-contain a#2 span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100'); } if (v2 === true){ $("div#small-obstacles-contain a#3 span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100'); $("div#small-obstacles-contain a#4 span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100'); } });


I suspect this is really an X-Y problem, this address the title, but may miss "what is really desired at the end of the day". In any case, the concepts are somewhat adaptable.

<hr />

I wouldn't use variables, but rather an an array/object.

Let's assume an object (so we can have different names used :-) and then a "control" sequence for which properties are eligible to be toggled:

var obj = {a: true, b: false, "3": false, hello: "world"} var control = ["a", "b", "3"] // set all to false -- noet that $.each != $().each !!! $.each(control, function (i, prop) { obj[prop] = false }) // set one true var trueIdx = Math.floor(control.length * Math.random()) obj[control[trueIdx]] = true

However, if variables were <em>really desired</em> for some reason then closures could be used (this could also be used to run arbitrary code for a particular binding):

var v1 = "hello" var v2 = "world" var control = [ function (v) { v1 = v }, function (v) { v2 = v } ] // set all to false -- noet that $.each != $().each !!! $.each(control, function (i, fn) { fn(false) }) // set one true var trueIdx = Math.floor(control.length * Math.random()) props[trueIdx](true)

Happy coding.


  • Creating PDF's using PHP
  • Translating X,Y Coordinate
  • Pivoting a table with predefined headers [closed]
  • How to Fire Windows Service stop event on System restart?
  • Left and right button misbehaving when trying to add an empty span to contenteditable div
  • Exception handling as per java coding standards
  • AngularJS : transclude ng-repeat inside directive
  • Whats the right place for testhelper-classes? (phpunit/best practise)
  • How to change default stop edit behavior in jtable
  • MySQL multiple IN conditions to subquery with same table
  • Specify HTTPS for custom WCF Binding
  • Adding independent aspx/asmx pages into DotNetNuke
  • SQL query to group by maximal sets of a column having inner consecutive distances below a threshold
  • Making Google Visualization - Annotation Chart to work in GWT
  • xtable - background colour of added rows
  • Problem with Django using Apache2 (mod_wsgi), Occassionally is “unable to import from module” for no
  • Find group of records that match multiple values
  • Center align outputs in ipython notebook
  • How can I set a binding to a Combox in a UserControl?
  • Calling Worksheet functions from vba in foreign language versions of Excel
  • Assign variable to the value in HTML
  • How to write order and limit within cakephp joins array
  • CakePHP 2.0.4 - findBy magic methods with conditions
  • Bad request using file_get_contents for PUT request in PHP
  • dc-js disable selecting slices on click for pie chart
  • Scrapy recursive link crawler
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • NetLogo BehaviorSpace - Measure runs using reporters
  • What is the “return” in scheme?
  • Fetching methods from BroadcastReceiver to update UI
  • How to handle AllServersUnavailable Exception
  • Symfony2: How to get request parameter
  • GridView Sorting works once only
  • R: gsub and capture
  • Android Studio and gradle
  • SQL merge duplicate rows and join values that are different
  • WPF Applying a trigger on binding failure
  • Benchmarking RAM performance - UWP and C#
  • Django query for large number of relationships
  • Net Present Value in Excel for Grouped Recurring CF