What does `$xml = $( xmlDoc )` do?

923 views Asked by At

In javascript / jQuery, the example on this page contains the following code which I am struggling to understand;

var xml = "<rss version='2.0'><channel><title>RSS Title</title></channel></rss>",
    xmlDoc = $.parseXML( xml ),
    $xml = $( xmlDoc ),
    $title = $xml.find( "title" );

Specifically the 3rd line;

$xml = $( xmlDoc )

What does that do? Does that form of syntax have a name that I can Google for to find out about it?

Also, in the code above they seem to be using the convention of prefixing variables that contain jQuery objects with a dollar sign. But if that's the case, then shouldn't the variable xmlDoc in the second line be $xmlDoc instead?

3

There are 3 answers

4
Asciiom On BEST ANSWER

It creates a jQuery object based on the xml specified above, enabling you to use jQuery's methods on it to find nodes and manipulate them.

1
xdazz On

It is to construct a jQuery object by a normal object. By doing this, you could use the jQuery method on it.

2
Curtis On

The $ symbol at the start of variable is purely just for naming convention (of jquery objects). It's a way of reminding you that this variable is a jquery object and can therefore have functions such as find() called on it.

$.parseXML( xml ) doesn't create a jQuery object, its just using jQuery to parse the XML.