-1

Please can anyone explain what this javascript code exactly does.

$('#element_id').html(response.title);

I need to access the value of the element_id but I can't using document.getElementById.

Thanks

MatthewMartin
  • 31,164
  • 31
  • 106
  • 162
Masood Hosseini
  • 193
  • 1
  • 8
  • 2
    http://stackoverflow.com/questions/846585/can-someone-explain-the-dollar-sign-in-javascript – Hashem Qolami Jun 29 '13 at 14:32
  • 1
    `"but I can't using document.getElementById"` - Why can't you? `document.getElementById` should get the element with the given ID. – David Jun 29 '13 at 14:32
  • what sort of element is this? a span, a text input box, a dropdown (which has a text and a value and a selected index). .val() gets many values, but value doesn't mean the same thing in all contexts. – MatthewMartin Jun 29 '13 at 14:35
  • 1
    [What is the XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) –  Jun 29 '13 at 14:38
  • Please ask about your actual problem. It seems you've deviated from your issue onto something tangentially related at best. –  Jun 29 '13 at 14:47

4 Answers4

2

This code just calls a function named $ and access a method of the returned object.

It's probably jQuery code due to the selector string.

  • $('#element_id'): Returns a jQuery object for the element with the given ID.
  • .html(response.title): Sets the inner HTML of the DOM element to response.title.

The raw JavaScript would look like this:

document.getElementById("element_id").innerHTML = response.title;
ComFreek
  • 28,220
  • 17
  • 99
  • 151
1

This code uses probably JQuery. The $ is the basic function defined by JQuery. You can call it to get access to element using a special query language defined by JQuery.

LaurentG
  • 10,440
  • 8
  • 47
  • 64
1

$ probably refers to jQuery, one of the most frquently used JS libraries.

What this snippet basically does is setting the HTML content of the element with the id element_id to the title attribute of the response object.

ComFreek
  • 28,220
  • 17
  • 99
  • 151
luk2302
  • 50,400
  • 22
  • 92
  • 131
1

It looks like jQuery, which is a Javascript library. The $('#element_id') creates a jQuery object for the element with the id element_id in the DOM. Then .html(response.title) will put the value of response.title as HTML inside the element.

gitaarik
  • 36,986
  • 11
  • 91
  • 97