Monday 15 March 2010

javascript - Can you instantiate an Element object AND define attributes simultaneously? -



javascript - Can you instantiate an Element object AND define attributes simultaneously? -

this may seem silly question some. short background is; clinically diagnosed ocd, particular formatting , neatness of code apologize in advance. leads me question:

is there way in javascript instantiate object , define attributes in block? goal might analogous how you'd see json object/string

here's pseudocode/formatting:

var preelement = document.createelement('pre') .classname = "noderesults"; .innerhtml = formattedresponse; .style = "blahblah"; .anymoreattributes = "stuff";

inconsequential, know. noticed spent 30 minutes researching instead of writing functional code. downfall of ocd. please help me; possible yes, or no?

sincerely, wastingtehtime

this not doable unless write class wrapper handle kind of formatting, or utilize jquery library. jquery example:

var preelement = $(document.createelement('pre')) .addclass("noderesult") .html(formattedresponse) .css(jsonformattedcss)

jsfiddle example: http://jsfiddle.net/3h5kfv2j/

this can implemented in vanilla javascript too, need find library or code yourself.

here sample code of vanilla implementation made: http://jsfiddle.net/4n4w3uqr/

javascript dom formatting element

No comments:

Post a Comment