$(this).attr('id') only for actual element

decibel

I use the following code for changing the icon in nested bootstrap collapsible elements (accordion). But when I click a nested element - e.g. the third level - the icons at the second and first parent elements will also be changed. I only want my click action to affect the element it was directly invoked on.

$(document).ready(function() {
    $('.collapse').on('show.bs.collapse', function() {
        var id = $(this).attr('id');
        $('a[href="#' + id + '"] .panel-title span').html('<i class="glyphicon glyphicon-chevron-down"></i>');
    });
    $('.collapse').on('hide.bs.collapse', function() {
        var id = $(this).attr('id');
        $('a[href="#' + id + '"] .panel-title span').html('<i class="glyphicon glyphicon-chevron-right"></i>');
    });
});

How can i change only the icon of the current element without the parent element?

nbrooks

The problem you're noticing isn't due to incorrect ID referencing, it's actually due to the event being triggered on the parent elements as well. You can fix this by preventing the event from 'bubbling' up the DOM tree (i.e. being triggered on all parent elements of your target element).

Prevent the event from bubbling up to the parent elements by calling stopPropagation on the event object.

From the jQuery docs:

Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.

$(document).ready(function() {
    $('.collapse').on('show.bs.collapse', function(e) {
        e.stopPropagation();
        var id = $(this).attr('id');
        $('a[href="#' + id + '"] .panel-title span')
            .html('<i class="glyphicon glyphicon-chevron-down"></i>');
    });
    $('.collapse').on('hide.bs.collapse', function(e) {
        e.stopPropagation();
        var id = $(this).attr('id');
        $('a[href="#' + id + '"] .panel-title span')
            .html('<i class="glyphicon glyphicon-chevron-right"></i>');
    });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

$(this).attr('id') only for actual element

From Dev

jquery attr add id to existing element

From Dev

Select the only Element with no class or ID

From Dev

Jquery element height as attr

From Dev

Why does jQuery select only one element when chaining .attr() in selector?

From Dev

jQuery show element ID only on homepage

From Dev

$(this).attr('id'); Not Working

From Dev

Dynamically add specific attr to element

From Dev

Extract attr values from Element

From Dev

Replace ::after with actual element

From Dev

Use content of an element in attr() of pseudo-element

From Dev

attr id with two words in javascript

From Dev

jQuery(this).attr('id') returns undefined

From Dev

attr id inside a cloned div

From Dev

how to store .attr("id") in a variable

From Dev

Why is .attr('id') undefined on click?

From Dev

attr('id') returns empty string

From Dev

attr id with two words in javascript

From Dev

jQuery find style attr of ID

From Dev

CSS selector to target element class or ID using only 1 statement

From Dev

Assign id to Input element which has only name attribute

From Dev

Find actual position of an element in WrapPanel

From Dev

Find actual position of an element in WrapPanel

From Dev

How to to mark actual element as an active

From Dev

which service converts element and attr names in AngularJS?

From Dev

Adding attribute to HTML element causing attr is not a function

From Dev

element[attr=value] selector doesn't work

From Dev

Getting element attribute with jQuery .attr() method

From Dev

ng-attr not evaluating on directive element