3

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?

Nigel Alderton
  • 2,265
  • 2
  • 24
  • 55
  • There's nothing special about the syntax. The variable named `$xml` is assigned the result of calling the function `$` with as its parameter the variable `xmlDoc`. It's the same syntax as `a = f(b)`. –  Aug 17 '12 at 10:11
  • "shouldn't the variable `xmlDoc` in the second line be `$xmlDoc` instead" - perhaps the author wanted to use `xml`/`$xml` but `xml` has been used already for the string. But nothing is mandatory about that. – pimvdb Aug 17 '12 at 10:13
  • @hvd So what does that line do? – Nigel Alderton Aug 17 '12 at 18:01
  • @NigelAlderton My comment only addressed the syntax part. The syntax just means "call function `$`", nothing special. But I'm not qualified to comment on what that function does. –  Aug 17 '12 at 18:37

3 Answers3

6

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.

Asciiom
  • 9,867
  • 7
  • 38
  • 57
  • Doesn't the previous line `xmlDoc = $.parseXML( xml )` return a jQuery object? What does the previous line return? – Nigel Alderton Aug 17 '12 at 18:05
  • $.parseXML(xml) returns an xml document, not a jQuery object – Asciiom Aug 26 '12 at 08:04
  • @Jeroen moons In this question; Could you please tell me; why all the four lines are comma separated (in single line)? This is not working when replace , with ;... :( – Kanagavelu Sugumar Jul 15 '13 at 11:14
  • It's a compound var declaration, if you want to do that on separate lines you can do ´var xmlDoc = $.parseXML( xml ); var $xml = $( xmlDoc );´ etc. – Asciiom Jul 17 '13 at 09:50
1

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.

Curtis
  • 101,612
  • 66
  • 270
  • 352
  • So you mean to say 'var jqXmlObj = $( xmlDoc )' is possible which is same as $xml = $( xmlDoc ) ?? – Kanagavelu Sugumar Sep 11 '13 at 14:39
  • 1
    @KanagaveluSugumar Yes, theres nothing special about having a dollar symbol in your variable name. `$xml` is no different to `foo` or `sau$age` – Curtis Sep 12 '13 at 09:42
0

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

xdazz
  • 158,678
  • 38
  • 247
  • 274