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?
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With