Link to home
Start Free TrialLog in
Avatar of cubicalmonkey
cubicalmonkey

asked on

How to parse text between HTML tags and set to variable value

Hello,
Using Javascript, how could I obtain everthing between the input tags and set it as a variable value?

The name and value attributes are dynamic and can change.


<INPUT TYPE=RADIO OnClick='doSubmit()' NAME=default_Q1_2_1927 VALUE=198301>The modem is <b>ONLINE</b> ALL LEVELS WITHIN SPEC</b>
<INPUT TYPE=RADIO OnClick='doSubmit()' NAME=default_Q1_2_1927 VALUE=198302>Modem <b>Offline</b> - (<b>SNMP Query failed</b>
<INPUT TYPE=RADIO OnClick='doSubmit()' NAME=default_Q1_2_1927 VALUE=198303>A different message is listed



For example, I want to parse out "The modem is <b>ONLINE</b> ALL LEVELS WITHIN SPEC</b>"
Avatar of basicinstinct
basicinstinct
Flag of Australia image

well this is all a bit confused, you see you normally don't include html within input tags, they are usually empty tags, like this (note the closing fwd slash):

<INPUT TYPE=RADIO OnClick='doSubmit()' NAME=default_Q1_2_1927 VALUE=198301/>

or you can write it like this:

<INPUT TYPE=RADIO OnClick='doSubmit()' NAME=default_Q1_2_1927 VALUE=198301></input>

as it is, where are the closing tags for your input tags?  you don't have any... that's naughty.

you could fix this up by closing the input tags and then putting the contents in a span tag next to them

then your solution would look like this:



<html>
<head>
<script type="text/javascript">
       function getNxtInnerHTML(obj)
      {
            var s = obj.nextSibling.innerHTML;
            alert(s);
      }
 </script>
</head>
<body>
<INPUT TYPE=RADIO OnClick='getNxtInnerHTML(this); doSubmit()' NAME=default_Q1_2_1927 VALUE=198301/><span>The modem is <b>ONLINE</b> ALL LEVELS WITHIN SPEC</b></span>
<INPUT TYPE=RADIO OnClick='getNxtInnerHTML(this); doSubmit()' NAME=default_Q1_2_1927 VALUE=198302/><span>Modem <b>Offline</b> - (<b>SNMP Query failed</b></span>
<INPUT TYPE=RADIO OnClick='getNxtInnerHTML(this); doSubmit()' NAME=default_Q1_2_1927 VALUE=198303/><span>A different message is listed</span>
</body>
</html>
Avatar of cubicalmonkey
cubicalmonkey

ASKER

My problem becomes more complicated...  This form is dynamically generated by a java applet (which I'm not familiar with nor have full control over) so adding <span> </span> after the <input> tag isn't really an option for me.  Unless its possible to do after the page loads? Something like find/replace???  
ASKER CERTIFIED SOLUTION
Avatar of basicinstinct
basicinstinct
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oh, we don't need this in there anymore:

 function getNxtInnerHTML(obj)
      {
            var s = obj.nextSibling.innerHTML;
            alert(s);
      }
Thank you!
This is exactly what I was looking for!
I'm having a problem getting this code working in IE.
It works perfectly in Firefox but I returns an unterminated constant string constant.
it seems IE does not like the " on this line.

inputs[i].onclick = new Function("var answers=('"+megatrim(newSpan.innerHTML.toString())+"');popinframe(answers);doSubmit();");

I've posted another question for help resolving this problem
https://www.experts-exchange.com/questions/22864952/unterminated-string-constant-on-in-IE-for-complex-function.html