0

This is my HTML

<td class=​"style1 product name" valign=​"top" nowrap>23$​</td>​, 
<td style=​"padding-left:​10px;​" class=​"product name" width=​"85%">productY</td>​
<td class=​"style1 product name" valign=​"top" nowrap>​10$​</td>​, 
<td style=​"padding-left:​10px;​" class=​"product name" width=​"85%">productX</td>​

I tried the script below, but this is returning the full html.

document.getElementsByClassName("product name")

How can i make it work so that it would return productX and productY in an array?

please let me know thanks

1
  • Consider looping through the array that .getElementsByClassName returns and using a conditional to push only elements you want to a new array. Commented Nov 16, 2016 at 22:20

5 Answers 5

1

Classes cannot contain spaces, so class=​"product name" is actually two classes: product and name.

Given your current HTML, you can use document.querySelectorAll('.product.name:not(.style1)') to grab the elements with classes product and name, while excluding those having class style1.

You can iterate through this list, grabbing the text content of each element like so:

var products = document.querySelectorAll('.product.name:not(.style1)'),
    a = [],
    i;

for(i = 0 ; i < products.length ; i++) {
  a.push(products[i].textContent);
}

console.log(a);
<table>
  <tr>
    <td class="style1 product name" valign="top" nowrap>23$</td>
    <td style="padding-left:10px;" class="product name" width="85%">productY</td>
    <td class="style1 product name" valign="top" nowrap>10$</td>
    <td style="padding-left:10px;" class="product name" width="85%">productX</td>
  </tr>
</table>

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

3 Comments

thanks, but i am getting undefined. However when i use this document.querySelectorAll('.product name:not(.style1)')[0].textContent; It is giving me the product 0 name. How can i add this in the loop to get the other products name?
Do you see undefined when you run the Snippet?
thanks for your help - this one worked - var x = Array.from(document.querySelectorAll(".product.name:not(.style1)"));
1

please try this one

var x = Array.from(document.querySelectorAll(".product.name:not(.style1)"));

console.log(x);
<table>
  <tr>
    <td class="style1 product name" valign="top" nowrap>23$</td>,
    <td style="padding-left:10px;" class="product name" width="85%">productY</td>
    <td class="style1 product name" valign="top" nowrap>10$</td>,
    <td style="padding-left:10px;" class="product name" width="85%">productX</td>
  </tr>
</table>

1 Comment

previously i use iteration using Array.protoype.filter.call(x,function(elm){...}), thanks to @RickHitchCock for his simplest syntax :not(.style1)
0

var elements = document.getElementsByClassName("product name") creates and array of elements with class name product name. Then you can acces this array like this elements[0]...etc.

Product name must be one word product-name because now your td has 3 classes

Comments

0

In your example, you have 2 classes applied to elements with class="product name". The first class is product and the second class is name. In HTML, multiple CSS classes are separated by spaces. Instead, try to use "product_name" (with an underscore).

HTML:

<td class=​"style1 product name" valign=​"top" nowrap>23$​</td>​, 
<td style=​"padding-left:​10px;​" class=​"product_name" width=​"85%">productY</td>​
<td class=​"style1 product name" valign=​"top" nowrap>​10$​</td>​, 
<td style=​"padding-left:​10px;​" class=​"product_name" width=​"85%">productX</td>​

Javascript:

document.getElementsByClassName("product_name")

1 Comment

Then use document.getElementsByClassName("product") and then of that group, check if each one also has a class of "name" as well. As written, it's 2 separate classes.
0

Add something like product-item as classname to your product elements:

var products = document.getElementsByClassName("product-items");
for (var i = 0; i<products.length; i++) {
    console.log(products[i].innerText);
}

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.