By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,286 Members | 2,443 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,286 IT Pros & Developers. It's quick & easy.

Click twice for function execution

P: 2
Hi.

I'm having some problems with the execution of a javascript function trigged by clicking on a div. This only occurs if I have not clicked anywhere on the page since load: if it is the first thing I click then I start getting problems.

See the problem in action here: http://willat8.freehostia.com/

If you click on the startbutton in the bottom left hand corner before you do anything else you'll notice that nothing happens. If you click it again then the intended action occurs.

I'll include the code for those who can't access the website.


Javascript

Expand|Select|Wrap|Line Numbers
  1. document.onclick = startMenuActions
  2.  
  3. var startMenuOn = false
  4. var clickedElement = "none"
  5.  
  6. function startOrbOnMouseOver() {
  7.  
  8. if (startMenuOn == false)
  9. {
  10. setBackground('startorb','url(http://willat8.freehostia.com/res/startorbon.png)')
  11. }
  12. }
  13.  
  14.  
  15. function startOrbOnMouseOut() {
  16.  
  17. if (startMenuOn == false)
  18. {
  19. setBackground('startorb','url(http://willat8.freehostia.com/res/startorboff.png)')
  20. }
  21. }
  22.  
  23.  
  24. function startMenuActions() {
  25.  
  26. document.getElementById("startmenu").onclick = function() {
  27. clickedElement = 'startmenu'
  28. }
  29. document.getElementById("startorb").onclick = function() {
  30. clickedElement = 'startorb'
  31. }
  32.  
  33. if (clickedElement == "startorb" && startMenuOn == false)
  34. {
  35. setBackground('startorb','url(http://willat8.freehostia.com/res/startorbclick.png)');setDisplay('startmenu','block');startMenuOn = true
  36. }
  37. else if (clickedElement == "startorb" && startMenuOn == true)
  38. {
  39. setBackground('startorb','url(http://willat8.freehostia.com/res/startorbon.png)');setDisplay('startmenu','none');startMenuOn = false
  40. }
  41.  
  42. if (clickedElement == "none")
  43. {
  44. setBackground('startorb','url(http://willat8.freehostia.com/res/startorboff.png)');setDisplay('startmenu','none');startMenuOn = false
  45. }
  46.  
  47. clickedElement = "none"
  48. }
Relevant HTML

Expand|Select|Wrap|Line Numbers
  1. <div id="startorb" onmouseover="startOrbOnMouseOver()" onmouseout="startOrbOnMouseOut()">
  2. </div>
Any help as to how I could make this function execute on the first click would be much appreciated.

Will.
Oct 25 '08 #1
Share this Question
Share on Google+
2 Replies


P: 93
Dear Friend,

See what is happenong in your code on the first click the function is getting fired
i.e startMenuActions it is going in that parenthesis it is setting the function clickedElement = 'startmenu' on onclick of the div insted of firing the function in which clickedElement = 'startmenu' is there.

On second click the startMenuActions is getting fired then it is firing the function clickedElement = 'startmenu' .Thats why it is getting fired on the second click.

So you just directly check the condition that which is element is fired.Instead of assigning any function to it.
Oct 25 '08 #2

P: 2
Dear Friend,

See what is happenong in your code on the first click the function is getting fired
i.e startMenuActions it is going in that parenthesis it is setting the function clickedElement = 'startmenu' on onclick of the div insted of firing the function in which clickedElement = 'startmenu' is there.

On second click the startMenuActions is getting fired then it is firing the function clickedElement = 'startmenu' .Thats why it is getting fired on the second click.

So you just directly check the condition that which is element is fired.Instead of assigning any function to it.
Thank you for your help Rsmastermind. Problem solved now :) (well, almost).
Oct 26 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.