Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
HTML
<button class="cl" id="1" >Try it</button>



JavaScript
var buttonId = 0;
var classId = 0;

function myFunction()
{

    for (i = 0; i < 10; i++)
    {
        var btn = document.createElement("BUTTON");
        btn.style.backgroundColor = "F093F5";
        btn.setAttribute("id", "btn" + buttonId);
        btn.setAttribute("class", "class" + classId);

         btn.addEventListener('click', keyFunction);
        btn.innerText = "click" + buttonId;
        btn.innerText = "click" + classId;
        console.log(btn);
        document.body.appendChild(btn);

        buttonId++;
        classId++;

    }
}

{
    var colors = ["#ea4747", "#eaa947", "#c9ea47", "#68ea47"];
    function keyFunction(event)
    {
        event.target.style.backgroundColor = "#D3D578";
        var id = event.target.id.replace("btn", "") * 1;
        event.target.style.backgroundColor = colors[id % 10];


}
}


What I have tried:

I did Generating 10 buttons at once for every click event that contains unique class and id.. And asking help for differentiate last clicked buttons and visited buttons not visited buttons.. What logic to do this?
Posted
Updated 5-May-16 0:58am

1 solution

Try this

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script>
        var buttonId = 0;
        var classId = 0;
        var visitedbuttons = [];
        var lastbutton;

       
        function myFunction() {

            for (i = 0; i < 10; i++) {
                var btn = document.createElement("BUTTON");
                btn.style.backgroundColor = "F093F5";
                btn.setAttribute("id", "btn" + buttonId);
                btn.setAttribute("class", "class" + classId);

                btn.addEventListener('click', keyFunction);
                btn.innerText = "click" + buttonId;
                btn.innerText = "click" + classId;
                console.log(btn);
                document.body.appendChild(btn);

                buttonId++;
                classId++;

            }
        }

        {
            var colors = ["#ea4747", "#eaa947", "#c9ea47", "#68ea47"];
            function keyFunction(event) {
               
                event.target.style.backgroundColor = "#D3D578";
               // var id = event.target.id.replace("btn", "") * 1;
                event.target.style.backgroundColor = colors[Math.floor(Math.random() * 3) + 1];
               
                for (var i = 0; i < visitedbuttons.length; i++) {
                    visitedbuttons[i].style.backgroundColor = 'brown';
                }
                if (lastbutton != null)
                    lastbutton.style.backgroundColor = 'yellow';

                lastbutton = event.target;
                visitedbuttons.push(lastbutton);
                 
            }
        }
       


    </script>
</head>
<body>

</body>
</html>
 
Share this answer
 
v2
Comments
Prasath 1992 6-May-16 7:28am    
Thank you.. It's working
Karthik_Mahalingam 6-May-16 11:27am    
welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900