Using jquery hide get the id clicked

Greeting,

I'm using jquery to show and hide faqs and answers. Every thing works as it should. Where I need the assistance is modifying the script to return the id of the h2 clicked. But only when its shown.  

HTML

<div class="faqs">
    <h2 id="question1' class="questionLink">Question 1</h2>
    <div class="faqs-content">
        answer
    </div>
    <h2 id="question2' class="questionLink">Question 2</h2>
     <div class="faqs-content">
        answer
    </div>
</div>

Thanks
var $a = jQuery.noConflict();
$a(function () {
    $a('.faqs > .faqs-content').hide();
    var toggles = $a('.faqs > h2');
    toggles.addClass('toggle');
    toggles.click(function () {
        var toggled = $a(this).next();
        var $anextDiv = toggled.next();
        var $avisibleSiblings = $anextDiv.siblings('.faqs-content:visible');
        if (toggled.hasClass('expanded')) {
            $a('.expanded').removeClass('expanded');
            $anextDiv.slideToggle(500);
        } else {
            $a('.expanded').removeClass('expanded');
            if ($avisibleSiblings.length) {
                $avisibleSiblings.slideUp(500, function () {
                    $anextDiv.slideToggle(500);
                });
                toggled.prev().addClass('expanded');
            }
            else {
                $anextDiv.slideToggle(500);
                toggled.prev().addClass('expanded');
            }
        }
    });
});

Open in new window

GrantBaileyAsked:
Who is Participating?
 
HainKurtSr. System AnalystCommented:
use this

alert(this.id);

in your code... your sample is not working fine for me... is this how it should work?
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

<script>
//$(document).ready(function() {

var $a = jQuery.noConflict();
$a(function () {
    $a('.faqs > .faqs-content').hide();
    var toggles = $a('.faqs > h2');
    toggles.addClass('toggle');
    toggles.click(function () {
        var toggled = $a(this).next();
        var $anextDiv = toggled.next();
        var $avisibleSiblings = $anextDiv.siblings('.faqs-content:visible');
        if (toggled.hasClass('expanded')) {
            $a('.expanded').removeClass('expanded');
            $anextDiv.slideToggle(500);
        } else {
            $a('.expanded').removeClass('expanded');
            if ($avisibleSiblings.length) {
                $avisibleSiblings.slideUp(500, function () {
                    $anextDiv.slideToggle(500);
                });
                toggled.prev().addClass('expanded');
            }
            else {
                $anextDiv.slideToggle(500);
                toggled.prev().addClass('expanded');alert(this.id);
            }
        }
    });
});

//});
</script>

<div class="faqs">
    <h2 id="question1" class="questionLink">Question 1</h2>
    <div class="faqs-content">
        answer 1
    </div>
    <h2 id="question2" class="questionLink">Question 2</h2>
     <div class="faqs-content">
        answer 2
    </div>
</div>

Open in new window

0
 
leakim971PluritechnicianCommented:
let me understand... if you don't see it how can you click on it?

else to filter you can use the :visible or :hidden selector

http://api.jquery.com/visible-selector/
http://api.jquery.com/hidden-selector/

so perhaps (yes perhaps because I did not fully understand your need) replace line 4 by :


var toggles = $a('.faqs > h2:visible');

Open in new window

0
 
DesignbyonyxCommented:
Just as a tip for FAQ, you can use a definition list for more semantic and lighter markup:

<dl>
  <dt id="q1"><a href="#q1">Put the question here</a></dt>
  <dd><p>Put answer #1 here</p></dd>
  <dd><p>Put answer #2 here</p></dd>

  <dt id="q2"><a href="#q2">Put another question here</a></dt>
  <dd><p>Put answer here</p></dd>
</dl>

Unfortunately, you can't put a heading inside of the DT element because the DT element is an inline element.  You can put anything you want inside the DD element.  Anyhow, moving forward with you question.  You are doing quite a bit of unnecessary work with all of your hiding/showing logic.  Here is a plugin I use for all of my FAQ pages.  I also modified it so you can find the ID of the item just clicked.  You may need to modify to suite your needs.
(function($) {
	$.fn.faqcordion = function(options) {
		var settings = $.extend({}, $.fn.faqcordion.defaults, options);
		
		return this.each(function() {
			var $faq = $(this);
			var $dt = $faq.find('dt');
			var $dd = $faq.find('dd').hide();
			$faq.click(function(e) {
				var $targ = $(e.target);
				if($targ.is('dt > a')) {
					$targ = $targ.parent();
					/// HERE IS THE ID OF THE CURRENT DT ITEM
					alert('You just clicked on question with ID: ' + $targ[0].id);
				}
				if($targ.is('dt')) {
					$targ.toggleClass(settings.dtActiveClass);
					while($targ.next().is('dd')) {
						$targ = $targ.next().slideToggle(settings.animationDuration);
					}
					return false;
				}
			}).find('dt').hover(function() {
				$(this).addClass(settings.dtOverClass);
			}, function() {
				$(this).removeClass(settings.dtOverClass);
			}).css('cursor', 'pointer');
			
			if(settings.showExpandAll === true) {
				var $expand = $('<a href="#expand" class="expandAll" title="' + settings.expandAllText + '">' + settings.expandAllText + '</a>');
				$expand.insertBefore($faq).click(function() {
					if($expand.text() == settings.expandAllText) {
						$expand.text(settings.collapseAllText);
						$dt.addClass(settings.dtActiveClass);
						$dd.css('display', 'block');
					} else {
						$expand.text(settings.expandAllText);
						$dt.removeClass(settings.dtActiveClass);
						$dd.css('display', 'none')
					}
					return false;
				});
			}
		});
	};
	
	$.fn.faqcordion.defaults = {
		showExpandAll: true,
		expandAllText: 'Expand All',
		collapseAllText: 'Collapse All',
		dtOverClass: 'over',
		dtActiveClass: 'active',
		animationDuration: 200
	};
}(jQuery));

Open in new window

0
 
DesignbyonyxCommented:
Also, you can use jQuery noConflict in a much better method so that you can write code with the dollar sign syntax.

var $a = jQuery.noConflict();

// This is known as "closure"
(function($) {
  // Now you can use the dollar sign syntax without breaking other scripts on your page
  $('#selector');
}($a));

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.