In javascript, I would like to declare the variable name that contains the index of the repeat statement.

Asked 2 years ago, Updated 2 years ago, 43 views

Currently, the function I implemented is implemented as shown in the image below.

Here's the function.

To implement this functionality

I used jequery+js code

(function(){
        $(document).ready(function(){
        // Gets the index of the last book item.
        //(If there are 3 books, the value of 3 is stored in $book_list_num).
        $book_list_num = $("div").data("listNum");

        for(i=1; i<= parseInt($book_list_num); i++){
           //What do you want to know?
            var btni = $("#btn"+i)
            console.log(btni)
        }
    });

})();

I wanted to make a button variable as many as the number of buttons generated in html to hold the button and use the onclick method to apply the effect, so I used the repetition statement in the repetition sentence. I'm not sure how to hold it with button+index.

Please give me some advice.

javascript django jquery

2022-09-22 08:18

1 Answers

I'm replying to you again.

You can use the evaluation.

eval("var btn" + i + " = '#btn" + i + "';");

I think we can put a variable that specifies the name of btni.

var vname = "#btn"+i; 
var btni = $(vname);


2022-09-22 08:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.