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
  • 707
  • 9
  • 23
  • 50

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,443
  • 7
  • 40
  • 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
  • 143,427
  • 25
  • 201
  • 198
2

Use .text() or .html()

$('#nameDisplay').text()

FIDDLE

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

Community
  • 1
  • 1
yashhy
  • 2,741
  • 5
  • 28
  • 54
0

If you required text without html then:

$('#nameDisplay').text()

if required with html tags then:

$('#nameDisplay').html()
Jain
  • 1,189
  • 9
  • 16