Jump to content

Plz Help..


mowais

Recommended Posts

Hey.. :rolleyes: I hope you All Good.. I am trying to make a program in javascript but it was not running..Plz find the mistake.. Here is the coding..

<html><head><title>Game</title><script type="text/javascript">var a=0;function ans(a)  {  if(a==10)   {    alert("Congratualtion !! You have Won 1000 Rupees");    return true;   }  else   {    return false;   }   }</script></head><body><script language="javascript">document.write("<div><h1>Aik ODI mein aik bowler maximum kitne over krwa sakta hai?</h1></div>");document.write("<div><h3>5</h3></div>");document.write("<div><h3>8</h3></div>");document.write("<div onClick='ans(10)'><h3>10</h3></div>");document.write("<div><h3>2</h3></div>");if(ans(a)== 1){document.body.innerHTML="";   document.write("<div><h1>Aik T-20 mein aik bowler maximum kitne over krawa sakta hai?</h1></div>");   document.write("<div><h3>6</h3></div>");   document.write("<div><h3>8</h3></div>");   document.write("<div><h3>10</h3></div>");   document.write("<div><h3>12</h3></div>");   }</script></body></html>

It is not running after if condition first Question is running but the second one is not running plz Help me... I'll be very thankful to you all... :Pleased:

Link to comment
Share on other sites

Javascript runs as soon as it is loaded. The moment you load the document, the result for ans(a) is not 1 because "a" is not defined so your if() block is ignored When you click the button, you execute the ans() function for 10. It will execute the alert statement and then do nothing else because that's all it was told to do.

  • Like 1
Link to comment
Share on other sites

HmMm..I got Foxy Mod plz correct it. I want that if i click on that div whom i set it onclick then if'll run otherwise not ..... Plz Help...

Link to comment
Share on other sites

Put all the code that should execute when the button is clicked into the click event handler. That is the function that runs when the button is clicked. document.write() will clear the whole page before writing so it's preferable that you change the page in a different way, for example setting the innerHTML of an element.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...