JQuery: get identifiers from class

I have a lot of divs and I want to fade away this who is hanging.
How can I get the id of the hanging div?
Is there any way to do this other than the call function (and send id) using "onmouseover"?
Thanks!

+3
source share
3 answers

try something like

$(".classes").mouseover(function() {
    $(this).function();
};

to get the id of the element using the attr function

$('.name').attr('id');
+14
source

Hover over these divs ...

HTML:

<div class="add_post_cmmnt" id="box-100" >Box1</div>
<div class="add_post_cmmnt" id="box-200" >Box2</div>
<div class="add_post_cmmnt" id="box-400" >Box3</div>

JavaScript:

 $(".add_post_cmmnt").hover(function(e) {

      var id = this.id; // Get the id of this

      console.log("id is " + id); //Test output on console 

      $('#pid').val(id); // Set this value to any of input text box

      $(this).fadeOut(400); // Finally Fadeout this div

});
+2
source

, divs , , div.

(function($){
    $.fn.extend({ 
        myDivHover: function(){
            var $set = $(this);
            return $set.each(function(){
                var $el = $(this);
                $el.hover(function(){
                    fadeOutAnimation( $el, $set );
                }, function(){
                    fadeInAnimation( $el );
                });

            });
        }
    });

    function fadeOutAnimation( $target, $set ){

        // Revert any other faded elements
        fadeInAnimation( $set.filter('.hovered') );

        // Your fade code here
        ...
        ...

        // Flag
        $target.addClass('hovered');
    }

    function fadeInAnimation( $target ){

        // You revert fade code here
        ...
        ...

        // Unflag 
        $target.removeClass('hovered');
    }

})(jQuery);

// Apply it to the divs with XXX class
$('div.XXX').myDivHover();

// Select hovered item
var theID = $('div.XXX').filter('.hovered').attr('id');

, :)

0

Source: https://habr.com/ru/post/1725498/


All Articles