Solved

How to get correct .innerHTML result

Posted on 2000-03-29
4
135 Views
Last Modified: 2011-09-20
Hi!

Problem: I need exact HTML inside given <SPAN> tag, .innerHTML property strips quots around attribute values.
Environment: ie5+

Do you know a way to get that HTML as is?

my task is to transform client-side-edited piece of HTML4 with XSL. XMLDOM object requires 'correct' xml initialization like <DIV CLASS="node">...</DIV>, but .innerHTML just yelds <div class=node>...</div> instead. It behaves better with complex values like CLASS="node it is", but such classnames spoil CSS. :(
0
Comment
Question by:drom
  • 2
  • 2
4 Comments
 
LVL 7

Expert Comment

by:nettrom
ID: 2668623
seems to me that IE lives by the HTML spec, quotes are optional.

are you looking for a way to have quotes around all attribute values, or simply class?  if it's only class you could pull it out using object.className (http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/classname.asp)

another workaround would be to do use regular expressions (I believe IE5 supports those) and add single/double quotes around all attributes through a quick'n'dirty s/(\w)+=(\w+)\s/$1="$2"\s/;
0
 
LVL 1

Author Comment

by:drom
ID: 2668695
thanks, nettrom.
I need well-formed xml that means no attributes without value and all values quoted.
sure, I can trepanate everything with DOM on my own, but it's not too pleasant.
anyway, thank you :) your regexp looks promising if it will work for tag attributes only.
0
 
LVL 7

Accepted Solution

by:
nettrom earned 125 total points
ID: 2668773
yeah, I know about those well-formed restrictions, so I knew you had to get the values quoted.

I read up a bit about this on the MSDN site, and tested it out with IE5.  a quick solution that only works with attributes containing word-characters (a-z, A-Z, 0-9 if I remember correctly) works as follows:

  var myValue = myObject.innerHTML;
  var breakPoint = myValue.indexOf(">")+1;
  var startTag = myValue.substring(0,breakPoint);
  var regExp = /(\w+)=(\w+)/g;
  var result = startTag.replace(regExp, "$1=\"$2\"");
  var newElement = result + myValue.substring(breakPoint);

very explicit programming here, rough example.  I'm not sure how familiar you are with regular expressions.  I'll ellaborate & explain if you want me to.
0
 
LVL 1

Author Comment

by:drom
ID: 2669196
I'm not too familiar with regular expressions, but your answer was enough. Next code does almost what I need:

var r = object.innerHTML;
var re = /(<[^>]*)(\w+)=([^\s">]+)([^<]*>)/g;
while(re.test(r)) r = r.replace(re, "$1$2=\"$3\"$4");

Thank you.
Dmitry.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
A form to still have contents even if some are wrong 10 58
Wrapper for APPs 9 76
bootstrap footer centering and columns alignement 3 58
HTML5 Get data in table rows 5 29
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question