Solved

How to get correct .innerHTML result

Posted on 2000-03-29
4
132 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
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 style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now