?
Solved

issue with jquery not executing a function

Posted on 2014-08-12
13
Medium Priority
?
397 Views
Last Modified: 2014-08-12
I have to develop a faq section, where users search for contents. When one searches for contents, it brings back some results and wanted to show and hide certain div.

http://shaperweb.com/sample/faq_json.html

When you enter a search term "man" this should bring you back with two results.

each results have the following structure.

<h5 class="plus" id="h5code">title of the faq</h5>
<div "display:none">description of faq</div>

I have written code for executing while some one clicks on h5.

      $('h5').on('click',function(e) {
            //e.preventDefault();
            //$(this).toggleClass('minus plus').next().toggle('slow', 'swing');
            //return false;
            alert ('hai');
      });

      $('#results h5 minus').next().show();
      $('#results h5 plus').next().hide();
      

Not sure why its not getting into the function.

So question is can you guys help me in 2 things.
1. Why is my function not executed?
2. How can i show and hide the following div, which will have the description of the faq?
0
Comment
Question by:Perumal_RM
  • 5
  • 3
  • 2
  • +2
13 Comments
 
LVL 23

Expert Comment

by:Snarf0001
ID: 40255366
It's throwing an error on the actual handler hookup.
You're using a very old version of jquery (1.4), the .on() function wasn't introduced until 1.7.

I'd suggest just upgrading to the most recent, but if you have to use this one, then change

$('h5').on('click',function(e) {

to

$('h5').click(function(e) {
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 40255391
The script block that includes the js_script.js file is outside the <body> tags.

Error from Firebug on the line with the handler:

TypeError: $(...).on is not a function
0
 

Author Comment

by:Perumal_RM
ID: 40255455
Hi snarf & Tom,

Did the changes you guys requested. Still doesnt help. Any ideas?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40255533
Just looked at the "view source" of the link.  It looks like you have two links to the jquery file.  Why not just use one link from the CDN:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

Open in new window

0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 40255546
If you insist on using that old version of query then you can try this:

       
$('h5').live('click', function(e) { 
		//e.preventDefault();
		//$(this).toggleClass('minus plus').next().toggle('slow', 'swing');
		//return false;
		alert ('hai');
	});

Open in new window


.click does not work for dynamically created elements and .on will not work with that version of jquery.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40255555
Here is the simplest jQuery / AJAX "Hello World" exercise I could find.  Hopefully the article explains some of the thinking and design that you'll want to consider when you try to use jQuery.
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html
0
 
LVL 58

Accepted Solution

by:
Gary earned 2000 total points
ID: 40256399
Use this code for your click

$("#results h5").live("click",function(){
	$(this).next().toggle()
})

Open in new window

Don't know what the minus/plus are supposed to be referencing since they're is nothing on the page for them
0
 

Author Closing Comment

by:Perumal_RM
ID: 40256929
It worked like a charm. Easy one and you gave the complete code. Thank you very much
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 40257072
I gave you the code as well in an earlier post. The key was to use .live instead of click.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40257075
The question wasn't about that
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 40257153
My comment resolved part 1 of a two part question.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40257162
Click Request Attention at the top unless the OP comes back to the Q
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 40257180
I know how it works.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Find out what you should include to make the best professional email signature for your organization.
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…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

850 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