0

I have created a page with two drop-down menus containing various values. Now I would like to add a "randomize" button. When clicked, this button would select any of the values at random in both fields. (the values are also copied on a box above each menu).

Project idea for drop down menus

So far I've coded the menus and the words display in the boxes above them when the user selects them. But now I'm trying to add a randomise button that would put any of the values in the drop down as selected and of course displayed in the above text box. Ideally, more values in the drop-down menus would be added every once in a while without making the script dysfunctional... and ideally it would all be contained in a HTML file (calling for JQuery or javascript is ok).

I've looked at this but it doesn't apply.

I also looked at this but it's not really a feature that the user activates.

Very grateful if anyone can help! Thanks

Community
  • 1
  • 1
MicroMachine
  • 179
  • 5
  • 16

5 Answers5

1

hope this helps HTML :

<select id="s1">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

<select id="s2">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

Javascript:

var minNumber = 0;
var maxNumber = 3;

function randomNumberFromRange(min,max)
{
    return  Math.floor(Math.random()*(max-min+1)+min);


}


$("#s2")[0].selectedIndex = randomNumberFromRange(minNumber, maxNumber);
$("#s1")[0].selectedIndex = randomNumberFromRange(minNumber, maxNumber);

I create this fiddle for you... https://jsfiddle.net/s59g8vdp/

  • This is interesting, but I don't see the randomise button – MicroMachine May 08 '15 at 00:41
  • I voted for this instead of mine because it is more similar to what you wanted. Simply add the last two lines as function, and add a button that calls that function and you're done - https://jsfiddle.net/aqpwcoju/1/ – Ziv Weissman May 08 '15 at 00:49
0

This will create random number and click on random div menu:

    $(function(){
$(".menu").click(function(){
    alert("you clicked "+$(this).text());
    });
}); 

function doSomthing(){

   var rand=Math.floor((Math.random() * 5));
   alert(rand+1);
   var ele = $(".menu").get(rand);
   $(ele).click();

}

fiddle example: link

Or you can use this example to put value in the menu, or wherever you want: link 2

Ziv Weissman
  • 4,400
  • 3
  • 28
  • 61
  • The links you have created are interesting, but this is not what I call a "drop-down" menu, since all the values are displayed on top of each other. Also, I want the values to be randomised in the menus and above text boxes, not in a pop-up window. Thanks a lot anyways! It's interesting – MicroMachine May 08 '15 at 00:43
0

just a concept how to make that

$('input').on('click',function(){
    var randomnum = Math.floor(Math.random()*$('ul li').length);
    alert($('ul li').eq(randomnum).text());
});

DEMO FIDDLE

Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28
  • This is really cool, but the random value appears in a pop-up box. I want the value set in the drop-down menu to be changed. Thanks anyways! Interesting process – MicroMachine May 08 '15 at 00:44
0

The first example you have provided is very close to what you want to do. You just need to:

  1. Get a random value between 0 and options.length - 1.
  2. Get option[random] and set its selected property to true.
  3. Copy that option's .innerHTML or .value to the .innerHTML or .value of the label on top of the dropdowns.

This is all you probably need:

function randomizeSelect(selectId, fieldId) {

    var options = document.getElementById(selectId).children;

    var random = Math.floor(options.length * (Math.random() % 1));

    var option = options[random];
    option.selected = true;
    
    document.getElementById(fieldId).value = option.innerHTML; // You can use .value instead in both places
}

var values = {};

window.onload = function(e) {

    document.onchange = function(e) { // Event Delegation: http://davidwalsh.name/event-delegate
        var t = e.target;
        if(t.tagName == "SELECT")
            document.getElementById(t.id.replace("Select","Label")).value = t.children[t.selectedIndex].innerHTML;  
    }

    document.oninput = function(e) {
        var t = e.target;
        if(t.tagName == "INPUT") {
            if(values.hasOwnProperty(t.id))
                var options = values[t.id];
            else
                var options = document.getElementById(t.id.replace("Label","Select")).children;
        
            var currentValue = t.value;
      
            for(i in options) {
                if(options[i].innerHTML == currentValue) { // Can also use .value
                    options[i].selected = true;
                    break;
                }
            }  
        }
    }

    document.getElementById("randomize").onclick = function(e) {
        randomizeSelect("leftSelect", "leftLabel");
        randomizeSelect("rightSelect", "rightLabel");
    }
}
<input type="text" id="leftLabel" value="Left 1">

<select id="leftSelect">
  <option value="l1" selected>Left 1</option>
  <option value="l2">Left 2</option>
  <option value="l3">Left 3</option>
</select>

<input type="text" id="rightLabel" value="Right 1">

<select id="rightSelect">
  <option value="r1" selected>Right 1</option>
  <option value="r2">Right 2</option>
  <option value="r3">Right 3</option>
</select>

<button id="randomize">RANDOMIZE</button>
Community
  • 1
  • 1
Danziger
  • 19,628
  • 4
  • 53
  • 83
  • This is great! This is the closest I got to what I'm trying to achieve... I used your code, but it seems that parts of it make it impossible for the user to change the value in the menus by themselves. The script that displays the code in a text box also seems to fight with it? Here it is: http://fabulousrice.com/external-file/menu-dan.html – MicroMachine May 08 '15 at 01:31
  • I have edited the code to always keep the data being displayed in both the `input` and the `select` consistent (even when you type). – Danziger May 08 '15 at 02:29
  • You should also revise your JS code as you have a few errors appearing on the console and take a look at the [W3C Markup Validator](https://validator.w3.org/check?uri=http%3A%2F%2Ffabulousrice.com%2Fexternal-file%2Fmenu-dan.html&charset=%28detect+automatically%29&doctype=HTML5&group=0), as you have a few serious errors in your HTML. – Danziger May 08 '15 at 02:34
  • Thanks a lot for all the help... I cleaned up the code using W3C, but it still won't work... Depressing! Thanks for all the help, though, I really appreciate it. Here's the latest version using your updated suggestions: http://fabulousrice.com/external-file/menu-dan-4.html – MicroMachine May 08 '15 at 07:07
  • You should wrap part of the code in a `window.onload` handler to prevent your code from trying to access a DOM element before the DOM is fully loaded. I've updated the code. – Danziger May 08 '15 at 13:32
  • Hey Your version works great. I'll mark that it solved my problem, although Santiago was a close second! – MicroMachine May 09 '15 at 04:34
0

Randomize Button added see... ;) i hope this solve your problem!

https://jsfiddle.net/s59g8vdp/1/

Html :

<select id="s1">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

<select id="s2">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

<a id="randomize" href="#">randomize</a>

Javascript :

var minNumber = 0;
var maxNumber = 3;

$("#randomize").click(function(){
    $("#s2")[0].selectedIndex = randomNumberFromRange(minNumber, maxNumber);
$("#s1")[0].selectedIndex = randomNumberFromRange(minNumber, maxNumber);
});

function randomNumberFromRange(min,max)
{
    return  Math.floor(Math.random()*(max-min+1)+min);


}


$("#s2")[0].selectedIndex = randomNumberFromRange(minNumber, maxNumber);
$("#s1")[0].selectedIndex = randomNumberFromRange(minNumber, maxNumber);
  • Awesome! I just tried adapting it to my page but I think that it fights with another script that displays the text in boxes... Here's your page suggestions: http://fabulousrice.com/external-file/menu-santiago.html – MicroMachine May 08 '15 at 01:34
  • Hey Santiago! Thank you very much. I updated your results onto my test page, but still, no success! I'm having a hard time figuring what's wrong... http://fabulousrice.com/external-file/menu-santiago-1.html – MicroMachine May 09 '15 at 04:17