Changing CSS of a div on click inside a for loop JavaScript












0















I have a code to change a div's clip-path when it's clicked and I want the div's clip-path's to change in order of the array.



code:



for (let i = 0; i < branches.length; i++) {
$('.branch').click(function(){
$(this).css('clip-path', positions[i]);
});
}


However this code will only run for the final iretation of the loop. I have seen some examples on how this would be fixed by putting the function outside the loop. But it doesn't work in my example. So please, if anyone knows how I can fix this, help me.










share|improve this question























  • Do you want every .branch to change whenever any .branch is clicked, or what exactly is the logic you're looking for?

    – CertainPerformance
    Nov 25 '18 at 11:49













  • I want the branch div that is clicked to change it's clip-path to the one that is located in the positions array. So no matter wath div you click they will always tranform clip-path in order to build an image together.

    – CWilliams
    Nov 25 '18 at 11:58
















0















I have a code to change a div's clip-path when it's clicked and I want the div's clip-path's to change in order of the array.



code:



for (let i = 0; i < branches.length; i++) {
$('.branch').click(function(){
$(this).css('clip-path', positions[i]);
});
}


However this code will only run for the final iretation of the loop. I have seen some examples on how this would be fixed by putting the function outside the loop. But it doesn't work in my example. So please, if anyone knows how I can fix this, help me.










share|improve this question























  • Do you want every .branch to change whenever any .branch is clicked, or what exactly is the logic you're looking for?

    – CertainPerformance
    Nov 25 '18 at 11:49













  • I want the branch div that is clicked to change it's clip-path to the one that is located in the positions array. So no matter wath div you click they will always tranform clip-path in order to build an image together.

    – CWilliams
    Nov 25 '18 at 11:58














0












0








0








I have a code to change a div's clip-path when it's clicked and I want the div's clip-path's to change in order of the array.



code:



for (let i = 0; i < branches.length; i++) {
$('.branch').click(function(){
$(this).css('clip-path', positions[i]);
});
}


However this code will only run for the final iretation of the loop. I have seen some examples on how this would be fixed by putting the function outside the loop. But it doesn't work in my example. So please, if anyone knows how I can fix this, help me.










share|improve this question














I have a code to change a div's clip-path when it's clicked and I want the div's clip-path's to change in order of the array.



code:



for (let i = 0; i < branches.length; i++) {
$('.branch').click(function(){
$(this).css('clip-path', positions[i]);
});
}


However this code will only run for the final iretation of the loop. I have seen some examples on how this would be fixed by putting the function outside the loop. But it doesn't work in my example. So please, if anyone knows how I can fix this, help me.







javascript for-loop click this clip-path






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 11:42









CWilliamsCWilliams

207




207













  • Do you want every .branch to change whenever any .branch is clicked, or what exactly is the logic you're looking for?

    – CertainPerformance
    Nov 25 '18 at 11:49













  • I want the branch div that is clicked to change it's clip-path to the one that is located in the positions array. So no matter wath div you click they will always tranform clip-path in order to build an image together.

    – CWilliams
    Nov 25 '18 at 11:58



















  • Do you want every .branch to change whenever any .branch is clicked, or what exactly is the logic you're looking for?

    – CertainPerformance
    Nov 25 '18 at 11:49













  • I want the branch div that is clicked to change it's clip-path to the one that is located in the positions array. So no matter wath div you click they will always tranform clip-path in order to build an image together.

    – CWilliams
    Nov 25 '18 at 11:58

















Do you want every .branch to change whenever any .branch is clicked, or what exactly is the logic you're looking for?

– CertainPerformance
Nov 25 '18 at 11:49







Do you want every .branch to change whenever any .branch is clicked, or what exactly is the logic you're looking for?

– CertainPerformance
Nov 25 '18 at 11:49















I want the branch div that is clicked to change it's clip-path to the one that is located in the positions array. So no matter wath div you click they will always tranform clip-path in order to build an image together.

– CWilliams
Nov 25 '18 at 11:58





I want the branch div that is clicked to change it's clip-path to the one that is located in the positions array. So no matter wath div you click they will always tranform clip-path in order to build an image together.

– CWilliams
Nov 25 '18 at 11:58












1 Answer
1






active

oldest

votes


















2














You need a persistent outside variable in order to keep track of how many times a click has occurred - set the css for the appropriate positions index, and increment the index:



let clickNumber = 0;
$('.branch').click(function(){
if (clickNumber >= positions.length) return;
$(this).css('clip-path', positions[clickNumber++]);
})





share|improve this answer


























  • As much as this was helpful it seemed to assign each branch it's own pre-determined value from the array. I would like it to be that no matter what branch you click, it will always go to it's path in the array in order so that it looks like it's building itself, no matter of the order you click the branches. If you know of a way I could accomplish this please answer.

    – CWilliams
    Nov 25 '18 at 12:12











  • What you're looking for isn't clear, do you mean that on the ith click overall, that clicked branch will change to positions[i], or what?

    – CertainPerformance
    Nov 25 '18 at 12:17











  • Yeah that is pretty much what I want it to be. No matter the order you click the divs they will reposition themself in an order to build an image. In theory I want to create an image out of randomly generated polygons floating around so when you click them they build an image together.

    – CWilliams
    Nov 25 '18 at 12:21











  • Ok, see edit, use a persistent outside variable

    – CertainPerformance
    Nov 25 '18 at 12:27











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467080%2fchanging-css-of-a-div-on-click-inside-a-for-loop-javascript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You need a persistent outside variable in order to keep track of how many times a click has occurred - set the css for the appropriate positions index, and increment the index:



let clickNumber = 0;
$('.branch').click(function(){
if (clickNumber >= positions.length) return;
$(this).css('clip-path', positions[clickNumber++]);
})





share|improve this answer


























  • As much as this was helpful it seemed to assign each branch it's own pre-determined value from the array. I would like it to be that no matter what branch you click, it will always go to it's path in the array in order so that it looks like it's building itself, no matter of the order you click the branches. If you know of a way I could accomplish this please answer.

    – CWilliams
    Nov 25 '18 at 12:12











  • What you're looking for isn't clear, do you mean that on the ith click overall, that clicked branch will change to positions[i], or what?

    – CertainPerformance
    Nov 25 '18 at 12:17











  • Yeah that is pretty much what I want it to be. No matter the order you click the divs they will reposition themself in an order to build an image. In theory I want to create an image out of randomly generated polygons floating around so when you click them they build an image together.

    – CWilliams
    Nov 25 '18 at 12:21











  • Ok, see edit, use a persistent outside variable

    – CertainPerformance
    Nov 25 '18 at 12:27
















2














You need a persistent outside variable in order to keep track of how many times a click has occurred - set the css for the appropriate positions index, and increment the index:



let clickNumber = 0;
$('.branch').click(function(){
if (clickNumber >= positions.length) return;
$(this).css('clip-path', positions[clickNumber++]);
})





share|improve this answer


























  • As much as this was helpful it seemed to assign each branch it's own pre-determined value from the array. I would like it to be that no matter what branch you click, it will always go to it's path in the array in order so that it looks like it's building itself, no matter of the order you click the branches. If you know of a way I could accomplish this please answer.

    – CWilliams
    Nov 25 '18 at 12:12











  • What you're looking for isn't clear, do you mean that on the ith click overall, that clicked branch will change to positions[i], or what?

    – CertainPerformance
    Nov 25 '18 at 12:17











  • Yeah that is pretty much what I want it to be. No matter the order you click the divs they will reposition themself in an order to build an image. In theory I want to create an image out of randomly generated polygons floating around so when you click them they build an image together.

    – CWilliams
    Nov 25 '18 at 12:21











  • Ok, see edit, use a persistent outside variable

    – CertainPerformance
    Nov 25 '18 at 12:27














2












2








2







You need a persistent outside variable in order to keep track of how many times a click has occurred - set the css for the appropriate positions index, and increment the index:



let clickNumber = 0;
$('.branch').click(function(){
if (clickNumber >= positions.length) return;
$(this).css('clip-path', positions[clickNumber++]);
})





share|improve this answer















You need a persistent outside variable in order to keep track of how many times a click has occurred - set the css for the appropriate positions index, and increment the index:



let clickNumber = 0;
$('.branch').click(function(){
if (clickNumber >= positions.length) return;
$(this).css('clip-path', positions[clickNumber++]);
})






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 12:27

























answered Nov 25 '18 at 12:00









CertainPerformanceCertainPerformance

92.9k165384




92.9k165384













  • As much as this was helpful it seemed to assign each branch it's own pre-determined value from the array. I would like it to be that no matter what branch you click, it will always go to it's path in the array in order so that it looks like it's building itself, no matter of the order you click the branches. If you know of a way I could accomplish this please answer.

    – CWilliams
    Nov 25 '18 at 12:12











  • What you're looking for isn't clear, do you mean that on the ith click overall, that clicked branch will change to positions[i], or what?

    – CertainPerformance
    Nov 25 '18 at 12:17











  • Yeah that is pretty much what I want it to be. No matter the order you click the divs they will reposition themself in an order to build an image. In theory I want to create an image out of randomly generated polygons floating around so when you click them they build an image together.

    – CWilliams
    Nov 25 '18 at 12:21











  • Ok, see edit, use a persistent outside variable

    – CertainPerformance
    Nov 25 '18 at 12:27



















  • As much as this was helpful it seemed to assign each branch it's own pre-determined value from the array. I would like it to be that no matter what branch you click, it will always go to it's path in the array in order so that it looks like it's building itself, no matter of the order you click the branches. If you know of a way I could accomplish this please answer.

    – CWilliams
    Nov 25 '18 at 12:12











  • What you're looking for isn't clear, do you mean that on the ith click overall, that clicked branch will change to positions[i], or what?

    – CertainPerformance
    Nov 25 '18 at 12:17











  • Yeah that is pretty much what I want it to be. No matter the order you click the divs they will reposition themself in an order to build an image. In theory I want to create an image out of randomly generated polygons floating around so when you click them they build an image together.

    – CWilliams
    Nov 25 '18 at 12:21











  • Ok, see edit, use a persistent outside variable

    – CertainPerformance
    Nov 25 '18 at 12:27

















As much as this was helpful it seemed to assign each branch it's own pre-determined value from the array. I would like it to be that no matter what branch you click, it will always go to it's path in the array in order so that it looks like it's building itself, no matter of the order you click the branches. If you know of a way I could accomplish this please answer.

– CWilliams
Nov 25 '18 at 12:12





As much as this was helpful it seemed to assign each branch it's own pre-determined value from the array. I would like it to be that no matter what branch you click, it will always go to it's path in the array in order so that it looks like it's building itself, no matter of the order you click the branches. If you know of a way I could accomplish this please answer.

– CWilliams
Nov 25 '18 at 12:12













What you're looking for isn't clear, do you mean that on the ith click overall, that clicked branch will change to positions[i], or what?

– CertainPerformance
Nov 25 '18 at 12:17





What you're looking for isn't clear, do you mean that on the ith click overall, that clicked branch will change to positions[i], or what?

– CertainPerformance
Nov 25 '18 at 12:17













Yeah that is pretty much what I want it to be. No matter the order you click the divs they will reposition themself in an order to build an image. In theory I want to create an image out of randomly generated polygons floating around so when you click them they build an image together.

– CWilliams
Nov 25 '18 at 12:21





Yeah that is pretty much what I want it to be. No matter the order you click the divs they will reposition themself in an order to build an image. In theory I want to create an image out of randomly generated polygons floating around so when you click them they build an image together.

– CWilliams
Nov 25 '18 at 12:21













Ok, see edit, use a persistent outside variable

– CertainPerformance
Nov 25 '18 at 12:27





Ok, see edit, use a persistent outside variable

– CertainPerformance
Nov 25 '18 at 12:27




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467080%2fchanging-css-of-a-div-on-click-inside-a-for-loop-javascript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen