3

I would like to convert this javascript line of code to CSS, I've been searching the site and google but haven't got straight answer.

var riskCell20 = document.getElementById("risk.probablity.literal.l100").childNodes[2];

to something like

#risk.probablity.literal.l100:nth-child(2);

could not find the right syntax

7
  • 3
    Does that element has that ID of exactly that (with the periods)? Or are they classes? Your CSS string actually means selecting an element with ID risk with the classes probability, literal, l100 Commented May 7, 2018 at 16:34
  • 3
    Can we assume this element's childNodes consists of only element nodes? Because usually it contains text nodes alongside element nodes, and given the kind of document you're working with it's very likely this element contains text nodes which would completely throw off the index. Commented May 7, 2018 at 16:35
  • 1
    it'd be nth-child(3) since the nodes are numbered from 0. Commented May 7, 2018 at 16:35
  • The is is the whole string, it is used just like java package naming Commented May 7, 2018 at 16:40
  • That was ibm RTC, we can not change it Commented May 7, 2018 at 16:42

2 Answers 2

4

As the id has dots, which is the character for target a class, you could use the attribute selector.

[attribute="value"]

With it you can target an element's attribute, such as title, href, etc., and in this case its id.

Stack snippet sample

[id="risk.probablity.literal.l100"] :nth-child(2) {
  color: red;
}
<div id="risk.probablity.literal.l100">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>


Note, the nth-child is not zero based, the childNodes is, so if you want the third child (childNodes[2]), your nth-child should be 3 (nth-child(3))

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

Comments

1

Since the . character has a special meaning on a selector, you may have to escape it:

#risk\.probablity\.literal\.l100:nth-child(2);

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.