JavaScript Jquery code doesnt work on html output from Javascript jquery code doesn't work,jquery-code-doesn'

I have this Jquery function to mask my textbox:

  jQuery(function($){ //Mask textbox #hour with placeholder
   $("#hour").mask("9:99",{placeholder:"0"});
   $("#hourInTable").mask("9:99",{placeholder:"0"});
});

Works perfectly with this html code:

But when I try to do it in the textbox that has the ID hourInTable outputted by Jquery it doesnt mask anything:

jqTds[2].innerHTML = '<input type="text" name="hourInTable" id="hourInTable" value="00:00">';

This above code is called after a button press and the textbox hourInTable is placed somewhere on the page.

Placed this code direct into my html:

<input type="text" name="hourInTable" id="hourInTable" value="00:00">

And it worked, so its due to the html output in JS.

Thanks in advance.

Answer:1

Most likely it happens because when jQuery does the masking, the input is not yet present. Give the function a name and call it after you are sure that the innerHTML is placed.

Answer:2

try something like this

 jqTds[2].innerHTML = '<input type="text" name="hourInTable" id="hourInTable" value="00:00">';
 // call after text box is added
 $("#hourInTable").mask("9:99",{placeholder:"0"});

because #hourInTable is not present on DOM ready so it doesn't apply mask on it

call masking function after your dynamically created input is added

Answer:3

Could you please let me know why this code doesn't work? I put the value 1 or the value 2 in the text element but I don't get the result "this is 1" or "this is 2" respectively. It seems that the line ...

Could you please let me know why this code doesn't work? I put the value 1 or the value 2 in the text element but I don't get the result "this is 1" or "this is 2" respectively. It seems that the line ...

  1. javascript comparison function
  2. javascript comparison function sort
  3. javascript comparison function string
  4. javascript set comparison function
  5. javascript datetime comparison function
  6. javascript number comparison function
  7. javascript arrow function comparison

I have a contenteditable div like this: <form> <div name="new_post" class="post" contenteditable="true"></div> <button type="submit">Submit</button> </form>...

I have a contenteditable div like this: <form> <div name="new_post" class="post" contenteditable="true"></div> <button type="submit">Submit</button> </form>...

  1. removing everything after a character using excel
  2. removing everything after character
  3. excel removing everything after a character
  4. sed remove everything after pattern

so i'm new to javascript and jquery. Need some help on how to run the 2nd function after the 1st one has completed? The first function is a progress bar and the second one is a form that i want to ...

so i'm new to javascript and jquery. Need some help on how to run the 2nd function after the 1st one has completed? The first function is a progress bar and the second one is a form that i want to ...

  1. jquery function after page load
  2. jquery function after delay
  3. jquery function after load
  4. jquery function after function complete
  5. jquery function after .each() has completed
  6. jquery function after function
  7. jquery function after animate
  8. jquery function after append
  9. jquery function after fadeout
  10. jquery function after another
  11. jquery function after timeout
  12. jquery function after time
  13. jquery function after ajax load
  14. jquery function after click event
  15. jquery function after page load complete
  16. jquery function after click
  17. jquery function after load html
  18. jquery function after dom load
  19. jquery function after submit
  20. jquery function after addclass

This is purely for debugging purposes. Is there a single command like addEventListener('*', myHandler) in native js or $(document).on('*', myHandler) in jQuery that would listen for any event ...

This is purely for debugging purposes. Is there a single command like addEventListener('*', myHandler) in native js or $(document).on('*', myHandler) in jQuery that would listen for any event ...