Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recognize element is same which is clicked in each() function?

I want to do something like this :

<div class="show" >Element 1</div>
<div class="show" >Element 2</div>
<div class="show" >Element 3</div>

$("div.show").click(function () { 
        $current_show = $(this);
        $("div.show").each(function () {
            if ($(this) != $current_show ) //here i want to check current element is not same
            {
              $(this).text('Write something');
            }
        });
    })

Or is there another way to do this?

like image 400
Atul Avatar asked Mar 06 '26 21:03

Atul


2 Answers

No need for each(), you can use not() to avoid clicked element

$("div.show").click(function() {
  $("div.show").not(this).text('Write something');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="show">Element 1</div>
<div class="show">Element 2</div>
<div class="show">Element 3</div>
like image 93
Pranav C Balan Avatar answered Mar 09 '26 09:03

Pranav C Balan


If you are performing action other that setting same text then Modify .each() to filter current element:

$("div.show").not(this).each(....)

Otherwise you do not need to iterate over element for setting same text. Use solution provided by pranav.

like image 23
Milind Anantwar Avatar answered Mar 09 '26 11:03

Milind Anantwar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!