I have following code in my web page. I need to find the product name that appears after a span with class of pn
. When I run the code below, I get empty string from getFirstProduct
method and also an empty string from getSecondProduct
method. However, if I use following expression then I get the correct product name: $("#p0 span.pn")[0].nextSibling.data
or $("#p1 span.pn")[0].nextSibling.data
but this is using JavaScript and I was wondering if there is a pure jquery solution.
A demo for this question is at this URL: demo code
Question : What should be the correct selector in these two methods so they return Product 0
and Product 1
respectively?
Code to find product name
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id='p0'>
<div>some content</div>
<div></div>
<div><span class='t pn'>Product : </span>Product 0</div>
<div></div>
<div>some content</div>
<div>some content</div>
</div>
<div id='p1'>
<div>some content</div>
<div></div>
<div><span class='t pn'>Product : </span>Product 1</div>
<div></div>
<div>some content</div>
<div>some content</div>
</div>
<button type="button" onclick="var x = getFirstProduct(); alert(x);">Get First Product</button>
<button type="button" onclick="var x = getSecondProduct(); alert(x);">Get Second Product</button>
<script>
function getFirstProduct() {
return $("#p0 span.pn").next().text();
}
function getSecondProduct() {
return $("#p1 span.pn").next().text();
}
</script>
UPDATE 1
Based on answer given and also the post whose duplicate this was marked as, I came up with following code that works perfectly. Demo code for solution is at this URL: solution code demo.
The idea is to get the parent div of span with class of pn
and then get all types of nodes only under it i.e. one level down from parent only, which is done by contents()
method ( and not children()
method since children will ignore text nodes). This would give us two elements - span and the text following the span element since these two are the only immediate
children of parent div. The text node would be the name of Product that we are seeking.
When seeking text nodes in jquery, it seems that contents()
method will almost always be required.
Code that works
<div id='p0'>
<div>some content</div>
<div></div>
<div><span class='t pn'>Product : </span>Product 0</div>
<div></div>
<div>some content</div>
<div>some content</div>
</div>
<div id='p1'>
<div>some content</div>
<div></div>
<div><span class='t pn'>Product : </span>Product 1</div>
<div></div>
<div>some content</div>
<div>some content</div>
</div>
<button type="button" onclick="var x = getFirstProduct(); alert(x);">Get First Product</button>
<button type="button" onclick="var x = getSecondProduct(); alert(x);">Get Second Product</button>
<script>
function getFirstProduct() {
return $("#p0 span.pn").parent().contents().filter(function(index) { return this.nodeType === 3 ;}).text();
}
function getSecondProduct() {
return $("#p1 span.pn").parent().contents().filter(function(index) { return this.nodeType === 3 ;}).text();
}
</script>