Link to home
Start Free TrialLog in
Avatar of Perumal RM
Perumal RM

asked on

issue with jquery not executing a function

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?
Avatar of Snarf0001
Snarf0001
Flag of Canada image

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) {
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
Avatar of Perumal RM
Perumal RM

ASKER

Hi snarf & Tom,

Did the changes you guys requested. Still doesnt help. Any ideas?
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

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.
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.
https://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It worked like a charm. Easy one and you gave the complete code. Thank you very much
I gave you the code as well in an earlier post. The key was to use .live instead of click.
The question wasn't about that
My comment resolved part 1 of a two part question.
Click Request Attention at the top unless the OP comes back to the Q
I know how it works.