Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-24-2013, 08:25 AM   PM User | #1
Richieaxe
New to the CF scene

 
Join Date: Jan 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Richieaxe is an unknown quantity at this point
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>
Richieaxe is offline   Reply With Quote
Old 01-24-2013, 03:10 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,389
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
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
sunfighter is offline   Reply With Quote
Reply

Bookmarks

Tags
jquery, rand, random

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:22 PM.


Advertisement
Log in to turn off these ads.