Solved

Jquery issue accessing HTML object

Posted on 2014-09-26
5
216 Views
Last Modified: 2014-09-26
Hello,

I would like to have a function execute when the user clicks on a hyperlink, but I think I am somehow not getting the right object as the function never gets called.  Here is a snippet of the HTML:

<div id="menuitem2" class="menuitem">
<div id="mainMenu">
| 
<a id="1Menuitem" class="langmenu">Sommaire</a>
 | 
<a id="2Menuitem" class="langmenu">Le carreaux</a>
 | 
<a id="3Menuitem" class="langmenu">Le contenu</a>
 | 
<a id="4Menuitem" class="langmenu">Le panneau de Filtrage</a>
 |
</div>
</div>

Open in new window


And here is the function call:

$("#mainMenu a.langmenu").on('click', function(e){
		alert("here");
		e.preventDefault(); 
          // Call the scroll function
        goToByScroll($(this).attr("id")); 

});	

Open in new window


There is a "menuitem1" id, so I would like to target the a.langmenu inside of the div mainMenu.

Any help would be much appreciated.

Cheers,
0
Comment
Question by:1Cougar
  • 3
5 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
test page : http://jsfiddle.net/3t24sbg5/

wait for page load using :
$(document).(ready(function() {
    // code inside to run once the skeletton of the page is loaded
});
or
$(window).load(function() {
   // code inside to run once all elements of the page are fully loaded
});
0
 

Expert Comment

by:pradeep sain
Comment Utility
You should call the on click event on document.ready function like this


<html>

<head>


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
 <script type="text/javascript">
$(document).ready(function(){    
$("#mainMenu a.langmenu").on('click', function(e){
		alert("here");
		e.preventDefault(); 
          // Call the scroll function
        goToByScroll($(this).attr("id")); 

});	
});
</script>
</head>

<body>
<div id="menuitem2" class="menuitem">
<div id="mainMenu">
| 
<a id="1Menuitem" class="langmenu">Sommaire</a>
 | 
<a id="2Menuitem" class="langmenu">Le carreaux</a>
 | 
<a id="3Menuitem" class="langmenu">Le contenu</a>
 | 
<a id="4Menuitem" class="langmenu">Le panneau de Filtrage</a>
 |
</div>
</div>
</body>
</html>

Open in new window

0
 

Author Comment

by:1Cougar
Comment Utility
Hello and thank you for responding so quickly.

Well, this code works but only with the line "alert("document ready");"  If I take this line out then I do not get the alert "here".  If I keep it in then I get the alert "document ready" and the alert "here".

$(document).ready(function() {
	alert("document ready");

$("#mainMenu a.langmenu").on('click', function(e){
		alert("here");
		e.preventDefault(); 
          // Call the scroll function
        goToByScroll($(this).attr("id")); 

});	
						  });

Open in new window

0
 

Author Comment

by:1Cougar
Comment Utility
OK, I moved the code and it works now.  I was building dynamic innerHTML so maybe that was the issue.

Thanks and cheers!
0
 

Author Comment

by:1Cougar
Comment Utility
I take that back...actually I do have the same problem...when I remove the alert "document ready" I no longer have the alert "here".

??
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now