CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript frameworks (http://www.codingforums.com/forumdisplay.php?f=62)
-   -   How to randomize pics for a quiz (http://www.codingforums.com/showthread.php?t=286348)

Richieaxe 01-24-2013 08:25 AM

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>

sunfighter 01-24-2013 03:10 PM

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


All times are GMT +1. The time now is 04:06 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.