Solved

jQuery onClick not working

Posted on 2014-04-08
9
393 Views
Last Modified: 2014-04-08
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
Comment
Question by:tonelm54
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 39985411
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
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 39985562
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
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39985567
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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 

Author Comment

by:tonelm54
ID: 39985613
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
 

Author Comment

by:tonelm54
ID: 39985614
Sorry for the confusion, I thought it would be easier to debug the simpler code instead of the full wack
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39985619
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
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 39985622
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
 

Author Closing Comment

by:tonelm54
ID: 39985625
Thank you, thought everything loaded in jsfiddle as document.ready and couldnt do anything outside. But learn something new everyday. Thank you
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 39985697
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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question