0

I want to get the value inside this <p> tag using a jquery code which starts with a $ sign, i only had to make it work using innerHTML.

Here is my sample code:

<p id="nameDisplay">Sample Name</p>

Here is my innerHTML code that worked:

document.getElementById('nameDisplay').innerHTML

I want to learn how to code it using jquery, thanks in advance.

Marc Intes
  • 737
  • 9
  • 25
  • 51

4 Answers4

2

You can use html() to get the HTML contents of your paragraph as well as using # to target an element by id:

$('#nameDisplay').html();
Felix
  • 37,892
  • 8
  • 43
  • 55
2

You can use ID Selector (“#id”) to get the element and html() in place of innerHTML The jQuery selectors are one of its salient features, you can learn more here.

$('#nameDisplay').html()

For id selectors, jQuery uses the JavaScript function document.getElementById(), which is extremely efficient. When another selector is attached to the id selector, such as h2#pageTitle, jQuery performs an additional check before identifying the element as a match.

Adil
  • 146,340
  • 25
  • 209
  • 204
2

Use .text() or .html()

$('#nameDisplay').text()

FIDDLE

read more on difference between .text() and .html()

Community
  • 1
  • 1
yashhy
  • 2,856
  • 5
  • 31
  • 57
0

If you required text without html then:

$('#nameDisplay').text()

if required with html tags then:

$('#nameDisplay').html()
Jain
  • 1,209
  • 10
  • 16