0

I have created an XML file and can successfully parse the file in jQuery. The XML file consists of pairs of selectors and css/html manipulators.

For example: Here is the console.log.

found session

found child selector

attributes$(".monday td.session.m_2_title")

attributes.css("background-color", "#00ffff");

$(".monday td.session.m_2_title").css("background-color", "#00ffff");

Here is the example code:

    $(xml_Data).find("session").each(function() {
    console.log("found session");
    $(this).children().each(function(){
       var tag = this.tagName;  
       console.log("found child " + this.tagName);
       console.log("attributes" + $(this).text());
       if (this.tagName == "selector"){
            //$(this).next();
            console.log("attributes" + $(this).next().text());
            console.log($(this).text() + $(this).next().text());
            $(this).text() + $(this).next().text();
       }
    });
});

In a fit of wild optimism I thought this:

$(this).text() + $(this).next().text();

Would cause the script to run the concatenated strings a statement. Obviously this is wrong.

The the question is how do I put the xml data together to create and run the appropriate statement.

codepuppy
  • 1,130
  • 2
  • 15
  • 25
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – John Saunders Sep 10 '12 at 19:16
  • Change your image. Nothing seen there. Or it would be even better to copy/paste console output if possible – Viktor S. Sep 10 '12 at 19:20

1 Answers1

0

You need to create a tag with jQuery, put output of this line ($(this).text() + $(this).next().text()) into it and insert it into your document.

var script=document.createElement('script');
script.type='text/javascript';
$(script).html( $(this).text() + $(this).next().text());
$("body").append(script);
​

Or you may use eval("code in string here"), which is not recommended.

Or you may use function declaration like this: var foo = new Function ([arg1[, arg2[, ... argN]],] functionBody) where function body should be a string with your JS code

Viktor S.
  • 12,736
  • 1
  • 27
  • 52
  • That works!! Just trying to pick it apart to understand it, not quite sure how $(script).html( .... ) $("body").append(script); resulted in an inline style being created. But it did. You say eval is bad is there a reason for this? – codepuppy Sep 10 '12 at 19:49
  • Inline style? Frankly speaking you question is not clear. Those two lines do next thing: javascript code taken from XML is inserted into newly created – Viktor S. Sep 10 '12 at 20:32
  • Why eval is bad-I'm sorry, but just google that. I never use it, just do not need it, but I saw this warning many times and even read why it is bad, but I do not remember exact reasons as it is not important for me. [Here](http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea) is a post with this question – Viktor S. Sep 10 '12 at 20:34
  • Hi @FAngel Ok thanks for that. My inline style comment arose from looking at firebug and seeing the style added on the html panel as opposed to the style panel. I now realise that this happens whenever you apply jquery.css() I hadn't registered that fact before. – codepuppy Sep 10 '12 at 21:22