1

I am dynamically creating HTML items using jQuery. Though items are dynamically created but their attributes are note. I mean if I assign class and id in the $() call, the elements are created without those attributes. Even my browser is showing null attributes. Problem is recognized when I tried to populate drop down according to first drop down selection.

for (var i = 1; i <= prescriptionnum; ++i) {
    $("<select/>", {
        class: 'selectdoctor',
        name: 'selectdoctor' + i,
        id: 'selectdoctor' + i
    }).appendTo("#prescriptiondiv").after("<br/>");

    $("<input/>", {
        type: "text",
        class: "textinput",
        name: "textinput" + i,
        id: "textinput" + i
    }).appendTo("#prescriptiondiv").after("<br/><input type='file' id='imageinput'"+ i +" class='imageinput' /><br/><br/>");
}

If same drop-down is created with class selectdoctor using HTML its working and the browser also show attributes but not for the ones that jQuery creates.

3
  • Probably because self-terminated select tags are invalid. Commented Sep 13, 2011 at 5:49
  • @Tieson: jQuery should treat <select/> the same as <select>. Commented Sep 13, 2011 at 6:06
  • You might want to use jquery tmpl for such problem. Commented Sep 13, 2011 at 6:14

1 Answer 1

1

The <select>s seem to be coming out fine but you have a quoting problem with your <input type="file"> elements, you should be using this:

"<br/><input type='file' id='imageinput" + i + "' class='imageinput' /><br/><br/>"

instead of this:

"<br/><input type='file' id='imageinput'"+ i +" class='imageinput' /><br/><br/>"

Demo: http://jsfiddle.net/ambiguous/nUmNe/1/

As far as not getting any attributes goes, I would guess that you're using an older version of jQuery, something older than 1.4 most like. Consider this version of the fiddle that uses jQuery 1.3.2:

http://jsfiddle.net/ambiguous/JbEMW/

That seems to be matching exactly what you're seeing. Also, from the fine manual:

jQuery( html, props )         version added: 1.4
html A string defining a single, standalone, HTML element (e.g. or ).
props An map of attributes, events, and methods to call on the newly-created element.

So the format of the $() call that you're using wasn't added until version 1.4.

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

9 Comments

Yaa got the point.... Let me know reason Y I can't create input type as file using same approach. Thought its working in jsfiddle but showing as text in my browser. I had this my previous question. stackoverflow.com/questions/7385733/…
In this I have to create fields using html or after. Fields created using jquery are showing null values. Its working in jsfiddle but not in browser.
@Rahul: Are you having trouble with all the form elements or just the <input type="file"> ones?
I have same trouble with all elements as this method don't show attributes in form. I don't know how its working in jsfiddle and showing attributes their. Regarding input type file I can't create field using this method. I have put link for that doubt.
@Rahul: I don't know what you're doing different than the jsfiddle I'm as lost as you are.
|

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.