Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 390
Nigel Alderton Avatar asked Jul 26 '26 15:07

Nigel Alderton


2 Answers

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.

like image 136
Asciiom Avatar answered Jul 29 '26 05:07

Asciiom


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.

like image 39
Curtis Avatar answered Jul 29 '26 06:07

Curtis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!