Reputation: 395
i have function including while loop in my function first process is completed but not gone again in click event
$('body').on('click', 'button.expandcollapse', function() {
// var currenttrid = $("tr.selected").attr("data-id");
var parentid = $(".selected").data("id");
var parrentid = (parentid) - 1;
var rowclick = parseInt($(this).closest('tr').attr('data-id'));
var currentid = rowclick + 1;
if ($("tr[data-id='" + parrentid + "'] > td:first-child button").hasClass("expandcollapse")) {
var parentleft = $("tr[data-id='" + rowclick + "']> td:first-child").css("padding-left");
while (true) {
var chiledtrleft = $("tr[data-id='" + currentid + "']> td:first-child").css("padding-left");
if (chiledtrleft <= parentleft) {
break;
}
$("tr[data-id='" + currentid + "']").hide();
currentid = currentid + 1;
}
$("span", this).text
} else {
var parentleft = $("tr[data-id='" + rowclick + "']> td:first-child").css("padding-left");
while (true) {
var chiledtrleft = $("tr[data-id='" + currentid + "']> td:first-child").css("padding-left");
if (chiledtrleft <= parentleft) {
break;
}
$("tr[data-id='" + currentid + "']").show();
currentid = currentid + 1;
}
$("button").removeClass("expandcollapse");
$("span", this).text("-");
}
i want again it comes to click event now the else condition is continuously working but i want go to the click event after one step completd
Upvotes: 1
Views: 78
Reputation: 5622
In the end of the function You are doing
$("button").removeClass("expandcollapse");
So since there is no button with class expandcollapse
. It does not get triggered for second time
Checkout this Demo . Your code goes something like this , and dont work on second click.
So better plan a logic first before implementing.
You can use this selector to make it work (only and only if you are not adding the button dynamically)
$("button.expandcollapse").click(function(){
if dynamic then use your selector with this change for your second last line of code
$(this).removeClass("expandcollapse");
Suggestion : Better Use different class for .removeClass() at last . Why do you want to remove the same class with which you are triggering the event. Please use some other class for other functionalities in click function.
Upvotes: 1