2

I have HTML like:

<div id='content'>
  <div id='price'>$100</div>
  <div id='another'>something</div>
</div>

I can get content from id="content" like.

vl = document.getElementById("content").value;

In this id, I have another id="price". But I cannot get directly content from id="price".

How can I get content from id="price" through id='content'.Thank you.

Hai Tien
  • 2,929
  • 7
  • 36
  • 55

10 Answers10

3

jQuery:

$("#price").text();

javascript:

document.getElementById("price").innerHTML;

DEMO

Kiran
  • 20,167
  • 11
  • 67
  • 99
2

You can use as below :

vl = document.getElementById("price").innerHTML;

JSFiddle - Check it out

YOu can also use jquery using .html() or .text() method

Butani Vijay
  • 4,181
  • 2
  • 29
  • 61
2

Try,

$('#content').html().find('#price').html();
HB Kautil
  • 267
  • 1
  • 6
2

Try this,

var parent = document.getElementById('content');
var childNodes = parent.childNodes;
for(var i = 0, len = childNodes.length;i<len;i++){
    if(childNodes[i].id === "price") {
        alert(childNodes[i].innerHTML);
    }
}
msapkal
  • 8,268
  • 2
  • 31
  • 48
2

Using jquery,

 $("#content > #price").text();
ahalya
  • 184
  • 1
  • 10
1

You can get value of price directly as :

v2 = document.getElementById("price").value;
prady00
  • 721
  • 1
  • 6
  • 17
1

You need to access the child element through parent element... that is your question right.. i solve it with jquery DEMO

$(document).ready(function(){
  var tem = $('#content').children('#price').text();
    alert(tem);
});
kamesh
  • 2,374
  • 3
  • 25
  • 33
1

using Jquery,

v1= $('#content').html();
v2=v1.find('#price');
v3=v1.find('#another')
Nisam
  • 2,275
  • 21
  • 32
1

IDs should only be used when there is one of that item on the page. You will have no other chance as to re-assign your IDs, or alternatively using the class attribute.

Try this way to select,

var vl = document.getElementById("content").getElementsByClassName("price")[0];

HTML

 <div id='content'>
   <div class='price'>$100</div>
   <div class='another'>something</div> 
 </div>
Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76
1

Try this out:- http://jsfiddle.net/adiioo7/C3C4x/1/

var price = document.querySelector("#content #price").innerHTML;

console.log(price);
Aditya Singh
  • 9,512
  • 5
  • 32
  • 55