Reputation: 973
I have a button and a spinner div as
<button type = "button" value= "Make Current" class = "current_button"> </button>
<div class="place-left margin_L20 margin_T5 loading_spin_current" style="display: none">
<small>Updating... <i class="fa fa-refresh fa-spin"></i></small>
</div>
i am Using ruby on rails to loop and show the multiple buttons and i want to show the spinner infront of the button which is being clicked by the user. this is what i have done
$('.current_button').on('click', function() {
$('.loading_spin_current').show();
});
but it shows the spinner in front of all button . please tell me how can i do it , thankx in advance!
Upvotes: 2
Views: 203
Reputation: 115222
$('.loading_spin_current')
selects all elements with class loading_spin_current
, you just want to select element immediately after the button so use next()
method
$('.current_button').on('click', function() {
$(this).next('.loading_spin_current').show();
});
next()
: Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector. ( Taken from https://api.jquery.com/next/ )
$('.current_button').on('click', function() {
$(this).next('.loading_spin_current').show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button type="button" value="Make Current" class="current_button"></button>
<div class="place-left margin_L20 margin_T5 loading_spin_current" style="display: none">
<small>Updating... <i class="fa fa-refresh fa-spin"></i></small>
</div>
<button type="button" value="Make Current" class="current_button"></button>
<div class="place-left margin_L20 margin_T5 loading_spin_current" style="display: none">
<small>Updating... <i class="fa fa-refresh fa-spin"></i></small>
</div>
<button type="button" value="Make Current" class="current_button"></button>
<div class="place-left margin_L20 margin_T5 loading_spin_current" style="display: none">
<small>Updating... <i class="fa fa-refresh fa-spin"></i></small>
</div>
<button type="button" value="Make Current" class="current_button"></button>
<div class="place-left margin_L20 margin_T5 loading_spin_current" style="display: none">
<small>Updating... <i class="fa fa-refresh fa-spin"></i></small>
</div>
<button type="button" value="Make Current" class="current_button"></button>
<div class="place-left margin_L20 margin_T5 loading_spin_current" style="display: none">
<small>Updating... <i class="fa fa-refresh fa-spin"></i></small>
</div>
<button type="button" value="Make Current" class="current_button"></button>
<div class="place-left margin_L20 margin_T5 loading_spin_current" style="display: none">
<small>Updating... <i class="fa fa-refresh fa-spin"></i></small>
</div>
<button type="button" value="Make Current" class="current_button"></button>
<div class="place-left margin_L20 margin_T5 loading_spin_current" style="display: none">
<small>Updating... <i class="fa fa-refresh fa-spin"></i></small>
</div>
<button type="button" value="Make Current" class="current_button"></button>
<div class="place-left margin_L20 margin_T5 loading_spin_current" style="display: none">
<small>Updating... <i class="fa fa-refresh fa-spin"></i></small>
</div>
UPDATE : If you want to toggle between show and hide then use toggle()
instead show()
$('.current_button').on('click', function() {
$(this).next('.loading_spin_current').toggle();
});
Upvotes: 2
Reputation: 25527
You can use .next()
method for getting the corresponding spinner element of the clicked button
$('.current_button').on('click', function() {
$(this).next().show();
});
Upvotes: 1