2

I'm trying to get the value of an element child. Here is the element :

<li id="1" style="display:normal">
    <h2 id="label_lg">Temp Set (º<span class="degsign">C</span>)</h2>
    <p id="ihi" style="color:teal;font-size:3vw;">--</p>
</li>

I would like to get the value of <p></p>, if possible in javascript. I've tried to use

document.getElementById(1).childNodes[1].nodeValue;

and

var o = document.getElementById(i).children
o[1].nodeValue

Nothing worked, the alert always returns null.

4
  • 2
    A paragraph doesn't have a value, it has innerHTML or innerText or even textContent Commented Jan 12, 2016 at 13:57
  • 2
    document.querySelector('#1 p').innerHTML Commented Jan 12, 2016 at 13:58
  • 3
    I suggest you do not use numeric IDs. Although valid in some versions of HTML, I do not recommend using them at all. Commented Jan 12, 2016 at 14:00
  • What is style="display:normal" supposed to be? normal is not a valid CSS property for display. Commented Jan 12, 2016 at 14:01

5 Answers 5

2

Since you've specified jQuery:

$('#1 p').text();

As simple as that.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use following:

innerText || textContent

These will give you text of element. If you have nested elements like a small tag inside p, you will get text of all.

innerHTML

This will return HTML of the element. If you have nested elements, it will not return text, but a raw html of that element. But if element does not have a child, then output will be same.

(function() {
  var parent = document.getElementById('1');
  var _innerText = parent.innerText;
  var _innerHTML = parent.innerHTML;
  var _textContent = parent.textContent;

  console.log(_innerText, _innerHTML, _textContent)
})()
<li id="1" style="display:normal">
  <h2 id="label_lg">Temp Set (º<span class="degsign">C</span>)</h2>
  <p id="ihi" style="color:teal;font-size:3vw;">--</p>
</li>

Comments

0

Try:

var pString = document.getElementById("ihi").value;

and to change the value:

pString.innerHTML = "Your string"

1 Comment

This is all kinds of wrong. document.getElementById("ihi").value is undefined because ihi, a p element, doesn't have a value property. document.getElementById("ihi").innerText would do the job, but it would return a string. You can set an innerHTML property on a string, but it's not going to have any effect on the UI. document.getElementById("ihi").innerText = "Your string" would work for that.
0

with JS

alert(document.getElementById('ihi').innerHTML)
<li id="1" style="display:normal">
      <h2 id="label_lg">Temp Set (º<span class="degsign">C</span>)</h2>
      <p id="ihi" style="color:teal;font-size:3vw;">--</p>
 </li>

with JQUERY:

alert($('#ihi').html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li id="1" style="display:normal">
      <h2 id="label_lg">Temp Set (º<span class="degsign">C</span>)</h2>
      <p id="ihi" style="color:teal;font-size:3vw;">--</p>
 </li>

Comments

0

You can use the innerText property:

alert(document.getElementById(1).childNodes[1].innerText);
// Alerts "--"

In case you want to use jQuery:

alert($("1").child("#ihi").html())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.