1

I am asking this question with respect to the following examples of code:

const celciusInput = document.querySelector('#celcius > input');
const fahrenheitInput = document.querySelector('#fahrenheit > input');
const kelvinInput = document.querySelector('#kelvin > input');

Another example:

<input type="radio" name="rate" value="Very poor"> Very poor
<input type="radio" name="rate" value="Poor"> Poor
<input type="radio" name="rate" value="OK"> OK
<input type="radio" name="rate" value="Good"> Good
<input type="radio" name="rate" value="Very Good"> Very Good

I have a hunch that the ">" operator in this context would cause JavaScript to extract some sort of data based on the defined class/id/etc. However, I am not sure if there is a name for the ">" operator in this context and what does it do exactly?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
asker
  • 11
  • 1
  • 4
    It's a CSS selector for direct/immediate descendent https://developer.mozilla.org/en-US/docs/Web/CSS/Child_combinator – Robert Apr 23 '21 at 00:36

1 Answers1

1

Good question! Sometimes it's difficult to tell when one language begins and another ends. For example, in your snippet:

const kelvinInput = document.querySelector('#kelvin > input');

This portion is Javascript:

const kelvinInput = document.querySelector();

And this portion is actually a "selector" that uses the child syntax (the ">" means "child").

'#kelvin > input'

Selectors are most often found in CSS to target and style particular HTML elements. In contrast, in your snippet the selector is being used so that Javascript can interact with some HTML.

When put together the snippet is saying: Create a Javascript variable named kelvinInput that references an HTML input field that is the child of an HTML element that has the id "kelvin".

Here's a snippet of code that illustrates how it might be used in context:

function update(){
  
  const kelvinInput = document.querySelector('#kelvin > input');

  document.getElementById('kelvin-output').innerHTML = kelvinInput.value;
  
  
}
<div id="kelvin">
  Kelvin (type a number here): <input type="text" onkeyup="update()">
</div>

<div id="kelvin-output"></div>
Luke
  • 2,751
  • 1
  • 17
  • 20
  • 1
    If you include the code from the JsBin here on Stack Overflow, perhaps using a Stack Snippet (icon looks like `<>` in the editor toolbar), you might be able to include a real link rather than a link disguised as code... – Heretic Monkey Apr 23 '21 at 01:34
  • 2
    As a point of total pedantry, but because this is about the intersection of languages, strictly speaking `#kelvin > input` is not CSS but _Selectors_. Note that the spec is called {"Selectors level 3"](https://www.w3.org/TR/selectors-3/) unlike CSS specs which are all titled with "CSS" at the start. E.g. ["CSS Flexible Box Layout Module Level 1](https://www.w3.org/TR/css-flexbox-1/). CSS _uses_ Selectors but so may other languages. Here, DOM is using Selectors. – Alohci Apr 23 '21 at 01:45
  • Thanks for those helpful comments. I've tried to address those suggestions and concerns in some edits to the answer. No one wants to be a pedant. But in this context pedantry is good! – Luke Apr 23 '21 at 02:04