9

I have an input element like: <input class="input-m" name="formelement[]" type="text">

I want to clone it and store cloned html to a variable:

var txt=jQuery("input[name="+n+"[]]:first").clone().html(); 

this returns an empty string.

How can I get the html content from .clone()?

1
  • Are you sure the selector works? try jQuery("input[name="+n+"[]]:first").length to check it Commented Jul 25, 2011 at 19:58

3 Answers 3

16

Try this

var txt=jQuery("input[name="+n+"[]]:first").clone().wrap("<div />").parent().html(); 
Sign up to request clarification or add additional context in comments.

1 Comment

How I can set value in cloned field using above syntax? I am trying different ways but not getting
2

I think you want the outer HTML instead of innerHTML:

var text = $('<div>').append(jQuery("input[name="+n+"[]]:first").clone()).html(); 

Comments

1

.html() returns the html inside the element. I am guessing you have something like <input name="..."/> which has no inner html.

You should probably rely on something like outerHtml plugin.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.