1

I`m having problem in getting an attribute from a xml object into JavaScript from classic asp. The following is my code:

if(len>0){
<%for xx=0 to SNodes.length-1%>//asp code
{
 //Javascript code
var IXmlNode=xmlnewObj.createElement("I");
IXmlNode.setAttribute("a",document.getElementById('a'+xx).value);
IXmlNode.setAttribute("X","<%=SNodes.item(xx).getAttribute("PP")%>");
xmlnewObj.documentElement.appendChild(IXmlNode); 
<% next %>//asp code
}
}

Here SNodes has xml like:

<tag><tag1 a="iii" PP="asdasdf"/><tag1 a="aaa" PP="asdasdf"></tag>

Only this line is troubling me:

IXmlNode.setAttribute("X","<%=SNodes.item(xx).getAttribute("PP")%>");

What is wrong with this line?

2 Answers 2

1

What kind of javascript are you trying to output in the first place? As it stand now you'll get something like this which would never work:

if (len > 0) {
    {
        var IXmlNode = xmlnewObj.createElement("I");
            IXmlNode.setAttribute("a", document.getElementById('a' + xx).value);
        IXmlNode.setAttribute("X", "PP");
        xmlnewObj.documentElement.appendChild(IXmlNode);
    }
    {
        var IXmlNode = xmlnewObj.createElement("I");
        IXmlNode.setAttribute("a", document.getElementById('a' + xx).value);
        IXmlNode.setAttribute("X", "PP2");
        xmlnewObj.documentElement.appendChild(IXmlNode);
    }
}

It could work like this, but I still doubt that's what you need:

if (len > 0) {
    (function() {
        var IXmlNode = xmlnewObj.createElement("I");
            IXmlNode.setAttribute("a", document.getElementById('a' + xx).value);
        IXmlNode.setAttribute("X", "asdasdf");
        xmlnewObj.documentElement.appendChild(IXmlNode);
    })();
    (function() {
        var IXmlNode = xmlnewObj.createElement("I");
        IXmlNode.setAttribute("a", document.getElementById('a' + xx).value);
        IXmlNode.setAttribute("X","asdasdf");
        xmlnewObj.documentElement.appendChild(IXmlNode);
    })();
}

You're best option is probably to turn it into a function and pass your asp variables into it:

if(len>0){
    <%for xx=0 to SNodes.length-1%>//asp code
    {
        xmlAppender(<%=xx%>, <%=SNodes.item(xx).getAttribute("PP")%>);
    }
    <% next %>
 }

var xmlAppender = function(i, childData) {
        var IXmlNode = xmlnewObj.createElement("I");
        IXmlNode.setAttribute("a", document.getElementById('a' + i).value);
        IXmlNode.setAttribute("X", childData);
        xmlnewObj.documentElement.appendChild(IXmlNode);
    };​
Sign up to request clarification or add additional context in comments.

3 Comments

:Thanks for the nice answer,that`s almost the trick,how ''xx'' is not replacing with actual values here?'xx' remains 'xx' only but not wit the value.
Oh, I see.. should be xmlAppender(<%=xx%>, <%=SNodes.item(xx).getAttribute("PP")%>); I'll edit the answer for that. I hope it works for you.
:It`s fixed.I just made 'xx' here a javascrpit variable and incremented there.Thanks for the answer.
0

Escape your quotes in the second parameter to the setAttribute function with backslashes, or use single quotes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.