2

I have a function that imports information into a form. I am using jQuery to place a date into an input box. The ID of the input box is "depart_date" and I am trying to insert the value of (let's say) 04/01/2012.

$('input#depart_date').replaceWith('<input class="input_med hasDatepicker" type="text" id="depart_date" name="depart_date" value="04/01/2012">');

This works just fine. However, I lose my datepicker. I have even tried to "reinitialize" it by using

$( "#depart_date" ).datepicker({ minDate: 0});

I just don't understand how to get my datepicker back.

2 Answers 2

5

You're not just changing the value, you're replacing the object entirely (and removing all the UI hooks [click, focus, blur events] along with it).

Try using just $('#depart_date').val('04/01/2012'); to assign the value. Or, better yet $('#datepart').datepicker('setDate', '04/01/2012');

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

3 Comments

I've tried using .val() and .html(), but neither seem to work. The value gets inserted, but it's the same situation with .replaceWith(), datepicker does not come back. I was just using .replaceWith() as an alternative to .val() and .html(). I've also tried .attr("value", "04/01/2012"). It seems that any value assignment will remove these hooks. Any ideas?
@SamPellino: Try this: $('#datepart').datepicker('setDate', '04/01/2012');
Just re-tried .val(), I was doing something wrong before - It actually works!
0

Why replace the input? Why not simply change the value?

$('input#depart_date').val("04/01/2012");

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.