here is the code

<div id="test">
    <input type="text" name="textfield" />
<div>

$('div#test').html() render like this along with endtag for />

<input type="text" name="textfield" ></input>

how can i render it like below

<input type="text" name="textfield" />

thanks in advance..!!

Recommended Answers

All 3 Replies

Is there a reason you want it like that? Not sure if there is an option to render it like that, but you can always put it in a variable and do a (regex) replace.

It must be a jQuery bug, so you should try a fix with them.
Input doesn't have a "closing tag" and most often it will cause DOM errors.

tip:
You don't use xhtml syntax when coding in HTML.

The jQuery documentation includes the following note for its .html() method :

This method uses the browser's innerHTML property. Some browsers may not return HTML that exactly replicates the HTML source in an original document. For example, Internet Explorer sometimes leaves off the quotes around attribute values if they contain only alphanumeric characters.

So the text returned by the method is synthetic and browser-dependent. jQuery makes no attempt to normalise it, and you can make no assumtions about which flavour of HTML is used.

I think it's reasonable to say that :

  • it is safe to re-inject an HTML string previously returned by .html() in the same browser (as will normally be the case)
  • it is probably unsafe to inject html returned by .html() in one browser, into another browser or into a document with a different doctype (eg via a server-side database).

It's also probably reasonable to say that an HTML string previously returned by .html() should be used for nothing other than immediate, same-document re-injection. As no assumptions can be made regarding the flavour of HTML returned, the string should not be manipulated. Instead, you can create an unappended (or appended) document fragment with $(htmlString) then manipulate the fragment, as required, with jQuery manipulation methods.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.