ProllyGeek
ProllyGeek

Reputation: 15836

javascript automatically closes tags in a string

This is a very weird issue

statements[bracket].assembly+='<expr operator="'+statements[bracket].firsthalf.mathoperator+'"><constant value="'+statements[bracket].firsthalf.constant+'" /><parameter name="'+statements[bracket].secondhalf.parameter+'"/></expr>'

the statements[bracket].firsthalf.constant=2 and the message should appear as <constant value="2" /> , but the tag is shown as <constant value="2"></constant> , any idea why the tag is closed automatically ?!

enter image description here

Upvotes: 0

Views: 93

Answers (2)

Guffa
Guffa

Reputation: 700332

The code doesn't produce the output that you show. Either you are actually using some other code, or you are doing something with the string (like parsing it as XML) before displaying it.

I provided the code with this data:

var bracket = 0;
var statements = [
    { assembly: '', operator: 'greater',
     firsthalf: { mathoperator: '*', constant: '2', parameter: '' },
     secondhalf: { mathoperator: '', constant: '2', parameter: '' }
    } ];

That makes the code alert this string, as expected:

<greater><expr operator="*"> <constant value="2" /> <parameter name=""/> </expr><parameter name=""/></greater>

Demo: http://fiddle.jshell.net/AMnLS/1/

Upvotes: 1

Lie Ryan
Lie Ryan

Reputation: 64847

You must have passed the expression through some place where the string is treated as HTML (e.g. innerHTML). The problem isn't in the string manipulation code you've shown but somewhere else.

What happens between the time you evaluate this expression to the time when you alert the statement?

Upvotes: 2

Related Questions