Getting element id with jquery

前端 未结 4 801
庸人自扰
庸人自扰 2021-01-27 15:24

The code should print the id of the selected div but it does not. I did not find the error. Thanks for help.

HTML


   
4条回答
  •  后悔当初
    2021-01-27 15:50

    Ok, I think I understand what you are missing. You are trying to log the ID after adding a row using add_row function,

    .form_row is added dynamically to the DOM. So when executing $('.form_row').click(, there is no .form_row to bind the handler. The below way of using .on method binds the handler to #form_area and executes the handler only when the click event is from .form_row

    $('#form_area').on('click', '.form_row', function () {
       console.log(this.id);
    });
    

    $('#form_area div') selects the div inside the div #form_area which doesn't have an ID

    Below comment in html shows which div is selected,

提交回复
热议问题