• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 400
  • Last Modified:

jQuery onClick not working

Im trying to make a div run a function when a div is clicked on by doing:-
<div class="menuItem" onClick="return runNavigate();">Item 1</div>

Open in new window


However when I try an run it in my code, it doesnt run the code:-
http://jsfiddle.net/63NUs/

What should happen is when the user clicks 'Main 1' the menu will appear which works, then the user clicks 'Tickets' which also works, but then when the user clicks on 'Item 1' it should run the function runNavigate which should (at the moment) just say 'Hi' which doesnt work.

No errors are displayed, just the click event is ignored. I have tried several options to try and disable child clicks following thought thinking that could cause the issue but still not found a solution.

Does anyone have any ideas what Im doing wrong here?

Thank you
0
tonelm54
Asked:
tonelm54
  • 3
  • 3
  • 2
  • +1
1 Solution
 
Julian HansenCommented:
Works when I copy the code to a standalone file
<!doctype html>
<html>
<head>
<style type="text/css">
.menuContainer { display:none; position: absolute; width: 200px; height: 300px; border: 1px solid black}
.menuButton { float:left; width: 60px; border: 1px solid red; margin: 3px; }
.menuItem { display:none; }
</style>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
  $('.menuButton').click(function () {
    if ($(this).next('.menuContainer').css('display') == 'block') {
      $(this).next('.menuContainer').css('display','none');
    }
    else {
      $('.menuContainer').css('display','none');
      $(this).next('.menuContainer').css('display','inline');
      $(this).next('.menuContainer') .position({
        of: $(this),
        my: "left bottom ",
        at: "left top"});        
    }
  });

  $('.menuSection').click(function () {
    if ($(this).children('.menuItem').css('display') == 'block') {
      //do nothing, children already showing
    }
    else {
      $('.menuItem').fadeOut().delay(400);
      $(this).children('.menuItem').fadeIn();    
    }
  }).children().click(function(e) { return false; });
});

function runNavigate() {
  alert('hi');
  return true;
}
</script>
</head>
<body>
<div class="menuButton">Main 1</div>
<div class="menuContainer">
  <div class="menuSection">Tickets
	<div class="menuItem">Item 1</div>
	<div class="menuItem">Item 2</div>
	<div class="menuItem">Item 3</div>
	<div class="menuItem">Item 4</div>
	<div class="menuItem">Item 5</div>
  </div>
    
  <div class="menuSection">Users
	<div class="menuItem" onClick="return runNavigate();">Item 1</div>
	<div class="menuItem">Item 2</div>
	<div class="menuItem">Item 3</div>
	<div class="menuItem">Item 4</div>
	<div class="menuItem">Item 5</div>    
  </div>
  <div class="menuSection">Assets</div>
  <div class="menuSection">Software</div>
</div>

<div class="menuButton">Main 2</div>
<div class="menuContainer">
    Hello Main 2
</div>

<div class="menuButton">Main 3</div>
<div class="menuContainer">
    Hello Main 3
</div>
</body>
</html>

Open in new window

0
 
Pierre CorneliusCommented:
You are not calling runNavigate in the click event handler

Change line 23:
from
}).children().click(function(e) { return false; });
to
}).children().click(function(e) { runNavigate(); });
0
 
Chris StanyonCommented:
You're loading all the code on page load. You shouldn't do that for functions that you need to run later, so you need to separate out the jquery onready functions and your runNavigate(). Usually in jQuery you use the document ready block:

$(document).ready(function() {

      $('.menuButton').click(function () {
         ...
      });

});

function runNavigate() {
   alert('hi');
   return true;
}

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
tonelm54Author Commented:
What Im trying to do is have a seperate onClick for each item eventually, just trying it on the first, so eventually it will look something like:-
        <div class="menuItem" onClick="return runNavigate(6,'Menu1', null, '_blank');">Item 1</div>
        <div class="menuItem" onClick="return runNavigate(5,'Menu1', null, 'divContents');">Item 2</div>
        <div class="menuItem" onClick="return runNavigate(5,'Menu1', null, 'divContents');">Item 3</div>
        <div class="menuItem" onClick="return runNavigate(4,'Menu1', null, 'dialogue');">Item 4</div>
        <div class="menuItem" onClick="return runNavigate(3,'Menu1', null, 'modal');">Item 5</div>    

Open in new window


So an updated jsFiddle will look like:-
http://jsfiddle.net/d3Mqy/1/

My problem is the onClick doesnt seem to fire with no parameters
0
 
tonelm54Author Commented:
Sorry for the confusion, I thought it would be easier to debug the simpler code instead of the full wack
0
 
Chris StanyonCommented:
My original point still stands. You need to move your runNavigate() function outside of the loading routine. Currently you have your JS set to 'onLoad', so it all gets runs when the page loads.

I've updated your code so that only the necessary bits run on load and your runNavigate function is outside of that block.

http://jsfiddle.net/d3Mqy/2/
0
 
Julian HansenCommented:
I think the problem has to do with your JSFiddle implementation - if you run the code I posted earlier it works.

Here is some modified code that does not use an onClick but rather attaches a click event via JQuery to the .menuItem div
<!doctype html>
<html>
<head>
<style type="text/css">
.menuContainer { display:none; position: absolute; width: 200px; height: 300px; border: 1px solid black}
.menuButton { float:left; width: 60px; border: 1px solid red; margin: 3px; }
.menuItem { display:none; }
</style>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
  $('.menuButton').click(function () {
    if ($(this).next('.menuContainer').css('display') == 'block') {
      $(this).next('.menuContainer').css('display','none');
    }
    else {
      $('.menuContainer').css('display','none');
      $(this).next('.menuContainer').css('display','inline');
      $(this).next('.menuContainer') .position({
        of: $(this),
        my: "left bottom ",
        at: "left top"});        
    }
  });

  $('.menuSection').click(function () {
    if ($(this).children('.menuItem').css('display') == 'block') {
      //do nothing, children already showing
    }
    else {
      $('.menuItem').fadeOut().delay(400);
      $(this).children('.menuItem').fadeIn();    
    }
  }).children().click(function(e) { return false; });
  $('.menuItem').click(function() {
	runNavigate($(this).data('id'),'Menu1', null, $(this).data('target'));
  });
});
function runNavigate(varType,varPost,varGet,varTarget) {
  alert('hi ' + varType + ' : '  + varPost + ' : ' + varGet + ' : ' + varTarget);
  return true;
}
</script>
</head>
<body>
<div class="menuButton">Main 1</div>
<div class="menuContainer">
  <div class="menuSection">Tickets
	<div class="menuItem">Item 1</div>
	<div class="menuItem">Item 2</div>
	<div class="menuItem">Item 3</div>
	<div class="menuItem">Item 4</div>
	<div class="menuItem">Item 5</div>
  </div>
    
  <div class="menuSection">Users
	<div class="menuItem" data-id="6" data-target="_blank">Item 1</div>
	<div class="menuItem" data-id="5" data-target="divContents">Item 2</div>
	<div class="menuItem" data-id="5" data-target="divContents">Item 3</div>
	<div class="menuItem" data-id="4" data-target="dialogue">Item 4</div>
	<div class="menuItem" data-id="3" data-target="modal">Item 5</div>    
  </div>
  <div class="menuSection">Assets</div>
  <div class="menuSection">Software</div>
</div>

<div class="menuButton">Main 2</div>
<div class="menuContainer">
    Hello Main 2
</div>

<div class="menuButton">Main 3</div>
<div class="menuContainer">
    Hello Main 3
</div>
</body>
</html>

Open in new window

Working sample here (http://www.marcorpsa.com/ee/t614.html)
0
 
tonelm54Author Commented:
Thank you, thought everything loaded in jsfiddle as document.ready and couldnt do anything outside. But learn something new everyday. Thank you
0
 
Julian HansenCommented:
Just to point out - my original post did include the onready code

$(function() {
...
});

Along with a note that the code worked.

No response to this was received however.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now