I need to create a label and text field on the fly and also include datepicker for the textfield. I need something like this:
<label for="from">From </label> <input type="text" id="from" name="from" />
I have tried something like this in jQuery:
var label = $("<label>").attr('for', "from");
label.html('Date: ' +
'<input type="text" id="from name="from" value="">');
$(function() {
$("#from").datepicker();
});
This one somehow doesn't create the label and also the text field. I am not sure what I am missing.
EDIT
To be more precise, I am using this in the portlets and I don't have body tags in the jsp page. So when I call the function to append to body it won't.
You will need to attach the label you've created to the DOM to get it to show:
Something like this would work:
jsFiddle Demo
Please note that you don't have to use the
for
attribute on the label if the input element is inside it. So use one of these:Where are you trying to insert the label? If you want it at the beginning of the page, you can do something like this.
1) You are missing closing quote at the end of the ID attribute.
2) You didn't append the label itself to anything.