Skip to main content
WolfShade
Legend
December 19, 2017
Answered

jQuery: reinit selector function when dynamically adding elements to DOM

  • December 19, 2017
  • 2 replies
  • 4775 views

Hello, all,

I'm using jQuery for many things on a page with a form, and I've encountered something that I'm not quite sure what the best way to approach it is.

For example, I'm using .clone() on a small section of the form (it's a cost estimate form in which the user can dynamically add items to the form) and part of the section contains checkboxes.  Depending upon which checkboxes are checked, certain selects (initially disabled) will become enabled and the user prompted to choose an option.  (Five checkboxes, four selects.)

At the bottom of the page, I have the following code to perform the above functionality whenever a checkbox is checked or unchecked:

$('.airLiner').on('click',function(){setRateType();}); // it just calls a function whenever a checkbox is changed.

The function has a lot of code, so for brevity I won't post all of it:

function setRateType(){

     for(var x = 0; x < cdLen; x++){

          var $selectedCksList = $('[name=airLiner-' + x + ']:checked').map(function(i,el){return el.value;}).get().join(',');// Creates a comma-delimited list of values.

          {switch/case: if itemA is found, enable select 'rateType' - else disable}

          {switch/case: if itemB is found, enable select 'movementType' - else disable}

          ...

          {switch/case: if itemG is found, enable select 'action' - else disable}

          }

     }

The issue is that if a user clicks the button to add an item, creating elements on the fly and adding to the DOM, the new checkboxes aren't initialized from when the page loads, like the first checkboxes are.

What is the best way to reinitialize, or whatever is best, the new checkboxes so that they are included in the onClick function?

Is this making sense?

V/r,

^ _ ^

    This topic has been closed for replies.
    Correct answer B i r n o u

    I'm not really sure to get what you mean... but if your problem is to get a portion cloned as it was initilay set (at start)  ... why don't you get a dummy dead hidden portion, like an HTML component set aside and clone that one. This portion will also be the only one ready to get the events... (and not having an already existing HTML portion of the form to be cloned)...

    second when you clone the element are you passing two boolean TRUE parameter... the first one for activating the data and the event, and the second one for the deep levels ?

    I'm not sure one to got your points, and second to be clear on my answer... please don't hesitate to let me know

    2 replies

    B i r n o u
    B i r n o uCorrect answer
    Legend
    December 20, 2017

    I'm not really sure to get what you mean... but if your problem is to get a portion cloned as it was initilay set (at start)  ... why don't you get a dummy dead hidden portion, like an HTML component set aside and clone that one. This portion will also be the only one ready to get the events... (and not having an already existing HTML portion of the form to be cloned)...

    second when you clone the element are you passing two boolean TRUE parameter... the first one for activating the data and the event, and the second one for the deep levels ?

    I'm not sure one to got your points, and second to be clear on my answer... please don't hesitate to let me know

    B i r n o u
    Legend
    December 20, 2017

    now if I miss understood your initial point, and if your trouble is only that the new created element wont get any event... instead of attaching event to the initial items, did you try to delegate them ?

    BenPleysier
    Community Expert
    Community Expert
    December 20, 2017

    In theory, there are two ways to tackle this, namely

    1. re-run the related function after the checkboxes have been added
    2. have the checkboxes available in a display:none element right from the start, to be displayed when needed.

    My preference would be the latter.

    Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
    WolfShade
    WolfShadeAuthor
    Legend
    December 20, 2017

    Hello, Ben,

    BenPleysier  wrote

    In theory, there are two ways to tackle this, namely

    1. re-run the related function after the checkboxes have been added
    2. have the checkboxes available in a display:none element right from the start, to be displayed when needed.

    My preference would be the latter.

    I've tried #1.  In the code after the .clone(), I run setRateType(); but it doesn't seem to be passing the event handler to the new checkboxes.  As far as #2, this would be great if I were limiting the user to just one additional item; but I have no idea how many items any given user may need to add, so I'm allowing unlimited ability to add items to the request.

    V/r,

    ^ _ ^