I’am trying to random select modal dialog and end after specific number of click.
I’am new at javascript and this is what i got so far.
<script type="text/javascript">
var randomNumber = Math.round(Math.random()*4+1);
function myclick(){
if (randomNumber == 1) {
$("#q1").click();
}
if (randomNumber == 2) {
$("#q2").click();
}
if (randomNumber == 3) {
$("#q3").click();
}
if (randomNumber == 4) {
$("#q4").click();
}
}
</script>
<body>
<a id="q1" href="#dialog1" name="modal" image=></a>
<a id="q2" href="#dialog2" name="modal" image=></a>
<a id="q3" href="#dialog3" name="modal" image=></a>
<a id="q4" href="#dialog4" name="modal" image=></a>
UPDATE!!!!!!!!!!!!!!!!!!!!!!!!
thanks for the fast reply.
Iam sorry not included all the script.
<script type="text/javascript" src="jquery.1.8.2.js"></script>
<script>
var randomNumber = Math.floor(Math.random()*4+1);
function myclick(){
if (randomNumber == 1) {
$("#q1").click()
}
else if (randomNumber == 2) {
$("#q2").click()
}
else if (randomNumber == 3) {
$("#q3").click()
}
else if (randomNumber == 4) {
$("#q4").click()
}
}
</script>
<BODY>
<h3>Web Design Quiz</h3>
<form name="quiz">
<a id="q1" href="#dialog1" name="modal" image=></a>
<a id="q2" href="#dialog2" name="modal" image=></a>
<a id="q3" href="#dialog3" name="modal" image=></a>
<a id="q4" href="#dialog4" name="modal" image=></a>
<input type="button" src="img/info.png" onclick="myclick()"></input>
I removed close”;” after “Round” and add “else if”. i still can only open 1 dialog at a time. I would like to open all dialog by clicking the button and end with the last one. how to refresh my random number everytime a click the button?
OK, firstly I hope you are aware you are using JQuery and have therefore including the correct scripts. Something like:
next for a list of issues:
Roundfunctionmyclickfunctionclickfunction is not ideal for simulating a click. It would be better to create a separate function that takes the selector id for the div that should be processedTIP: You can do this kind of thing with less code by custom building the JQuery selector which will save you having all the if statements. Something like:
Also, using multiple if statements like this should make use of the
else ifstatement too, because in this instance only one if statement can ever be successful