I have a plugin here that makes it easy to copy the attributes:

http://plugins.jquery.com/project/getAttributes

I just tested it on a checkbox in FF2/3 and Chrome, and I create a
checkbox with the copied attributes and append to the DOM, and the
checkbox is checked (you also get the checkbox if you use the :checked
selector, so jQuery sees the checked status). Interestingly though,
when you view the checkbox in Firebug the checked attribute is
missing, as in your example.



On Mar 19, 9:12 am, Muhammad Zaheer Asghar <zaheerasghar...@gmail.com>
wrote:
> http://lattestphones.blogspot.com/
>
> http://latesttechnologyblog.blogspot.com/
>
> http://home-safety-tips.blogspot.com/
>
> On 3/18/09, Greg Kass <greg.k...@ardishealth.com> wrote:
>
>
>
> > I have the following code (this is a minimized version of the actual code
> > to demonstrate what is happening).  The code inserts form fields from data
> > contained in an object:
>
> >        jQuery(document).ready(function() {
> >            var fieldName = "optin";
> >            var attrib = {
> >                "type":"checkbox", "className":"checked",
> > "checked":"checked", "value":"1", "name":"promo-optin", "id":"promo-optin"
> >            }
> >            var inputType = attrib.type;
> >            var inputHTML =
> > (inputType==="select")?"<select></select>":"<input type='"+inputType+"' />";
>
> >            inputHTML =
> > jQuery("<div>"+inputHTML+"</div>").find("input,select").each(function(){
> >                jQuery(this).attr("id",fieldName);
> >                for (x in attrib) {
> >                    if (x!=="type") {
> >                        jQuery(this).attr(x,attrib[x]);
> >                    }
> >                }
> >            }).end().html();
> >            jQuery("body").append(inputHTML);
> >        })
>
> > Every attribute in the attrib object is added to the input tag correctly in
> > IE6.  In FF2, Safari, and Chrome, the "checked" attribute is not added to
> > the tag, and therefore the checkbox is not checked.  (I haven't bothered to
> > check IE7 and FF3 yet)  Here is the actual tag from FF, retrieved via
> > Firebug:
>
> > <input type="checkbox" id="promo-optin" class="checked" value="1"
> > name="promo-optin"/>
>
> > Changing the value of "checked" from "checked" to true or "true" doesn't
> > make a difference, either.  How do I get the checked attribute to add
> > properly?

Reply via email to