Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to randomize pics for a quiz

    Hi, I need to randomize some pics of flags for a quiz.
    I'm kinda new to jQuery and can't figure out how to do this. Any help would be appreciated.

    <form action="#" method="POST" id="myform" name="myform"> <!-- The form -->
    <img class="flag" src="Flaggor/sveriges-flagga.png" alt="sverige"/> <!-- The diff.. flags -->
    <img class="flag" src="Flaggor/usas-flagga.png" alt="usa"/>
    <img class="flag" src="Flaggor/armeniens-flagga.png" alt="armenien" />
    <input type="text" id="answer" name="answer">
    <input type="button" id="send" value="Answer">
    </form>
    <div class="score">

    <p>Score:</p><br/>

    <input type="text" name="points" id="points" readonly="readonly" class="knapp"/>
    </div>
    <p id="result"></p>

    <script>

    var score = 0; // Holds the score

    $().ready(function() {

    $("#send").live("click",function() {
    var answer = $("#answer"); // Holds the answer value

    if(answer.val() == $(".flag:visible").attr("alt")) { // Shows flag

    $("#result").html("ok"); // If the answer is correct
    }else {
    $("#result").html("fel"); // If the answer is wrong
    }

    });
    changeFlag();
    function changeFlag()
    {
    var antalFlags = $(".flag").size(); // The number of flags
    var slumpad = 0;
    $(".flag").eq(slumpad).show(); // This is the part I can't figure out
    }
    });
    </script>

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,166
    Thanks
    23
    Thanked 601 Times in 600 Posts
    You shouldn't use the right names for your flag images nor for the alt attribute. Too easy to get the info on them. Use numbers instead. Than in javascript:
    Code:
    <script type='text/javascript'>
    var flag = Math.floor((Math.random()*10)+1);
    var flag_image = flag+".png";
    alert(flag_image);
    </script>
    Will randomize numbers between 1 and 10 and give you the image name as flag_image


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •