2 Replies - 148 Views - Last Post: 29 July 2015 - 12:28 AM

#1 Pedro_D  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-July 15

onClick function inside a if

Posted 28 July 2015 - 02:52 PM

Hello im trying to put a onclick function inside an if but it is not working and I cant solve it, I've already tryed event.target but id didn't work can someone help me.

The first onclick function count the number of click in the first button.

The second onclick2 function count the number of clicks in the second button, and I just want activate it after the if.

Thanks.

<script type="text/javascript">

var clicks = 0;
var clicks2 = 0;
var score = 0;
var expoint = 0;

function onclick() {

clicks += 1;
clicks2 += 1;
score += 1;

document.getElementById("clicks").innerHTML = clicks;
document.getElementById("clicks2").innerHTML = clicks2;
document.getElementById("score").innerHTML = score;

if(clicks > 0)
{
document.getElementById("btt1").style.visibility = 'hidden';

document.getElementById("btt2").style.visibility = 'hidden';

document.getElementById("btt3").style.visibility = 'hidden';

}
if(clicks2 > 14)
{ 
expoint =+ 1;
document.getElementById("expoint").innerHTML = expoint;

document.getElementById("btt1").style.visibility = 'visible';

function onclick2() {
expoint =- 1;

clicks += 2;
score += 4;

document.getElementById("bonus1").innerHTML = "BONUS x1";

document.getElementById("clicks").innerHTML = clicks + " x2";
document.getElementById("score").innerHTML = score + " x4";

document.getElementById("btt1").disabled = true;
    }
  } 
}
</script>




Is This A Good Question/Topic? 0
  • +

Replies To: onClick function inside a if

#2 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 913
  • View blog
  • Posts: 3,250
  • Joined: 15-January 14

Re: onClick function inside a if

Posted 28 July 2015 - 02:57 PM

You defined the function inside the other function, so it's only going to be defined inside that function. Define it outside the function so you can refer to it from another place.
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3775
  • View blog
  • Posts: 11,506
  • Joined: 08-June 10

Re: onClick function inside a if

Posted 29 July 2015 - 12:28 AM

word of caution: the onclick() function is likely used as click handler of window. I also see nowhere a proper event handler assignment.

Golden Rule: never name your functions/variables after some built-in methods or HTML IDs/classes/names.

Quote

I've already tryed event.target but id didn't work

there’s nowhere such code to see, so it’s likely that you missed the preconditions to it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1