When I click on .add-btn, I change the .add-btn's style to background:#cccccc and change the val() to "-".
Now when I remove the added text with tr td .list that is a row in a list, I want that .add-btn button that added the deleted row to style back to background:#232323 with val() "+".
The problem I have is that there is 10 buttons with classname .add-btn, and how will I now which of the 10 buttons that has been clicked, to add text?
jQuery("tr td .list").live('click', function() { jQuery(this).closest("tr td").remove(); }); jQuery(".add-btn").click(function(event) { jQuery(this).val("-"); jQuery(this).css("background", "#cccccc"); event.stopImmediatePropagation(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <tbody> <tr></tr><tr><td><div class="list"><p class="delete-list-domains">X</p>added text in a list</div></td></tr><tr></tr> </tbody> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn"> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn"> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn"> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn"> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn">
This is how you can target only a clicked button whilst adding the event to all of them in vanilla JS:
const btnEls = document.querySelectorAll('.add-btn')
Array.from(btnEls).forEach(btnEl => {
btnEl.addEventListener('click', () => {
// do whatever you want to button here (use btnEl var)
})
})
You have to assign different id for each button, giving same id is wrong.
$("tbody").on('click', '.list', function() { console.log($(this).find("tr")); var btn = $(this).data('id'); $('#'+btn).trigger('click'); $(this).remove(); }); $(".btn-xs").on('click', function(event) { var id = $(this).attr('id'); if($(this).hasClass('add-btn')) { $(this).removeClass('add-btn') .addClass('rm-btn') .val('-') .css('background','#cccccc'); $('tbody').append('<tr><td><div class="list" data-id='+id+'><span class="delete-list-domains">X</span> added text in a list by '+id+'</div></td></tr>'); } else if($(this).hasClass('rm-btn')) { $(this).removeClass('rm-btn') .addClass('add-btn') .val('+') .css('background','#fffff'); $('[data-id="'+id+'"]').remove(); } else { alert('error') } });
.delete-list-domains{ color:red;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table> <tbody id="content"> </tbody> </table> <hr/> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn1"> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn2"> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn3"> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn4"> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn5">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.