-1

I am using a jquery to show fields of HTML like select,text, image. But my issue as I am involving it using for loop, if once I select 5 than I select 3 using dropdown than I wish rest two fields should be removed. Second my image field is showing as text field.

 <select id="prescriptionnum"  >
      <option value="">Select Number</option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
  </select>

$('#prescriptionnum').change(function() {
    var childid = $("#childprescriptionselect").val();
    var prescriptionnum = $("#prescriptionnum").val();
    $('#prescription').show("slow");
    for (var i = 0; i < prescriptionnum; ++i) {

        $("<select/>&nbsp",{class:"selectdoctor"}).appendTo("#prescriptionform");       
        $("<input/>",{type:"text",class:"textinput"}).appendTo("#prescriptionform");
        $("<input/>",{type:"file",class:"imageinput"}).appendTo("#prescriptionform");


   }
});

I need good formatting that is space and enter between fields.

http://jsfiddle.net/xDAmf/

4
  • There is no #childprescriptionselect, #prescription or #prescriptionform. Html missing? Commented Sep 12, 2011 at 7:54
  • Can you provide the whole code? (what's in #prescriptionform?) Commented Sep 12, 2011 at 7:54
  • updated the url jsfiddle.net/xDAmf Commented Sep 12, 2011 at 7:59
  • They are the rest of html fields. I haven't described all here. Commented Sep 12, 2011 at 8:02

2 Answers 2

2

you can empty the prescription form before filling in it with html controls

$('#prescriptionnum').change(function() {
var childid = $("#childprescriptionselect").val();
var prescriptionnum = $("#prescriptionnum").val();
$('#prescription').show("slow");

//make the form empty before putting in contols
$("#prescriptionform").empty();
for (var i = 0; i < prescriptionnum; ++i) {

    $("<select/>&nbsp",{class:"selectdoctor"}).appendTo("#prescriptionform");       
    $("<input/>",{type:"text",class:"textinput"}).appendTo("#prescriptionform");
    $("<input/>",{type:"file",class:"imageinput"}).appendTo("#prescriptionform");


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

2 Comments

my button are showing as text field. I mean instead of browse I am getting text field to input.
try $("<input type='file' class='imageinput'/>").appendTo("#prescriptionform");
1

When the number of fields change, remove all items via:

$("#prescriptionform").children().remove();

Then add them back. You can use $.before() and $.after() to add additional markup such as &nbsp; or <br/>. Demo here.

4 Comments

Great its working good. How will I add id using loop. I mean for i=1 i want id as selectdoctor1
Y u have removed this. $("#prescriptionform").children().remove();
Yes, Parvesh's suggestion about using .empty() is better.
I am having text field at place of button. Suppose if I print button using jquery it will show me textfield. and I can input in that. Though its working fine in jsfiddle

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.