0

I wrote some simple code :

var tm='<input type="checkbox" name="city[]" />';

tm=$(tm).val('2');

alert($(tm).text()); // <--- it does not have internal text so how do i see itself?

but when im alerting it alert me empty message.

I want to see :

<input type="checkbox" name="city[]"  value="2"/>

in the ALERT

2 Answers 2

1

You want .html().

alert($("<div>").append(tm).html());

http://jsfiddle.net/z4AHG/

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

1 Comment

That won't work either. The input element has no children and the element itself is not included.
0

I found it

var tm='<input type="checkbox" name="city[]" />';

tm=$(tm).val('2');

alert( $('<div>').append(          tm.clone()              ).remove().html());

5 Comments

.remove() is unnecessary. The div element is not part of the DOM tree anyway.
@Felix Kling , I think it would be safer , since I can attache an event to the element , and i want to remove EVERYTHING
@Royi: It depends on what you want to do with the jQuery object... passing it to jQuery again is definitely not needed.
@Felix Kling I Talked about the .clone - , I asked if you thought it is necessary
@RoyiNamir: And I'm saying it depends on the context. If the element is already part of the DOM, then yes, clone would be necessary as you otherwise remove it from the its current location. In your situation here, it is not necessary.

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.