1

this image is my app post detail page this app is service about booksaling so user can post about booksale

enter image description here

the feature is

  • userA create a post (he want sale book1, book2, book3)
  • if book2 was sold out userA should click sold out button
  • when sold out button clicked, sold out will converted click cancel and strike line will added book2 (like book2(2222원))

so i decide using js+ jquery

here's my js code

(function(){

$(document).ready(function(){
    //$book_list_num is received number of books
    //book1, book2, book3//$book_list_num = 3 
    $book_list_num = $("div").data("listNum");

    console.log($book_list_num);

    var $cancel_button = []

    for(i=1; i<= parseInt($book_list_num); i++){

        //$cancel_button[0]=#btn1,
        //$cancel_button[1]=#btn2,
        //$cancel_button[2]=#btn3
        $cancel_button[$cancel_button.length] = $("#btn"+i);


        $cancel_button[i].on('click', function(event){
           //test alert
           alert(i)
        });
    };
});

})();

but web browser console show error

cancelbutton.js:14 Uncaught TypeError: Cannot read property 'on' of undefined

I can not understand why js can't read onmethod please somebody help me

3
  • @Rayon wont help that's a jQuery already if exists Commented Aug 18, 2016 at 6:44
  • @Rayon - the elements in the $cancel_button array are already jQuery objects. But OP hasn't allowed for array indices being zero-based. Commented Aug 18, 2016 at 6:47
  • @nnnnnn — Ooh yes! Commented Aug 18, 2016 at 6:56

2 Answers 2

1

Instead of trying adding click event listener in for loop can you please try following code:

$("[id^=btn]").on('click', function(event){
    var id = $(this).attr("id");
    var number = id.replace("btn", "");
    //test alert
    alert(number)
});

Here we are attaching click handler for all the DOM elements whose id starts with "btn". Hope it will help you a bit.

Sign up to request clarification or add additional context in comments.

3 Comments

"You can not put your click event handler in for loop." - Why not?
sorry @nnnnnn. Actually i wanted to recommend another way of doing it. Updated the wording.
@vijayP YOU SOLVED MY TERRIBLE PROBLEM THANK YOU VERY MUCH!! I'm so happy about your fantasic solution! I was almost crazy because function + loop in js is so hard to me,,, thank you!
0

It comes from here:

$cancel_button[i].on('click', function(event){
   //test alert
   alert(i)
})

Change to:

for(var i=0,l = parseInt($book_list_num);i<l; i++){

    //$cancel_button[0]=#btn1,
    //$cancel_button[1]=#btn2,
    //$cancel_button[2]=#btn3
    $cancel_button[i] = $("#btn"+(i+1));


    $cancel_button[i].on('click', function(event){
       //test alert
       alert(i)
    });
};

1 Comment

You've spotted the problem, but more explanation explaining the subtle changes you've made would be nice. (Of course, having fixed this the OP is about to have another problem, with the alert displaying the same value for every item.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.