开发者

How can I operate on an DOM element cloned with jQuery?

I have a form that I'm trying to duplicate using jQuery. I'm using the clone() method, which returns the cloned object (DOM element). I need to then select elements of the cloned form and manipulate them. Is this possible? If开发者_运维百科 so, how?

I've tried stuff like:

var clonedForm = $("#myForm").clone();
clonedForm.$(".inputField").val();

But (unsurprisingly) the second line doesn't work. Any help would be appreciated, thanks.


I think

$(clonedForm).find('.inputField').val()


If you want to change the Id of the element you are cloning, try this

var clonedForm = $("#myForm").clone();

clonedForm.attr( { id: 'new-id' } );


Well first what you get from the clone method is a piece of DOM that needs to be attached somewhere to be visible (it's not obvious from your code snippet if that's the case). Second - if you are using same IDs fir your elements you out of luck since findElementBYID will return you fisrt element it will find. You probably need go through your clone object ans change ID values. And then your syntax is also wrong in the second line as been pointed out


Some might find this more elegant

$(".inputField", clonedForm).val();

the second parameter specifies the context of the css selector.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜