2

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.

0

3 Answers 3

4

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.

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

1 Comment

Perfect worked, stupid me for forgetting this. Thanks!
1

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

Comments

1

Add the masking code in function. And call it on button click which adds dom <input type="text" name="hourInTable" id="hourInTable" value="00:00"> in page.

Comments

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.