?
Solved

How to get correct .innerHTML result

Posted on 2000-03-29
4
Medium Priority
?
141 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
Suggested Courses

765 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