0

i am new to JavaScript and i do not know what does following mean:

  $("#left-pane-tare-button a").show();

$('div#port-picker a.connect').text(getMessage('noconnect')).addClass('active');
12345GG
  • 101
  • 8

1 Answers1

2

This is not really a jQuery problem, it is a CSS selector problem.

"#left-pane-tare-button a"

The selector above is looking for an element with an id of left-pane-tare-button and selecting all the <a> tags that are descendants of the <div>.

A space means you are selecting the descendant of the element before the space.

As for

'div#port-picker a.connect'

A . means it is looking for a class.

In this case, it is looking for a <div> with the id port-picker and it is selecting all the descendant <a> tags that have the connect class.

For example:

<div id="port-picker">
    <a class="connect">
    <a class="foo">
    <a class="connect">
</div>

In this example, it will select the first and third <a> tag.

Here are some good resources to learn more about CSS selectors:

UPDATE: It appears I was wrong about the > as noted in this answer.

Example:

<div id="port-picker">
    <a class="connect">
    <a class="connect">
    <a class="foo">
    <div>
        <a class="connect">
    </div>
</div>

Using

'div#port-picker a.connect'

will select ALL <a> tags that are descendants of <div>

However

'div#port-picker > a.connect'

will only select THE FIRST THREE <a> tags that are children of <div id="port-picker">, because the fourth one is not a direct child of the original <div>.

No Name
  • 612
  • 6
  • 15
  • 2
    Three corrections: *1*) `"#left-pane-tare-button a"` is not looking for a `div`, it is only checking for the `id` attribute. *2*) "A space means you are selecting the child of the element before the space." --- this is incorrect. A space is the "descendant selector", meaning "p em" is selecting _all_ `em` that are descendants of `p` no matter how deeply they are nested. *3*) The `>` is the "direct child combinator", meaning `p > em` will select all `em` that are _direct_ children of a `p` tag (not grandchildren, or further). You may be confusing this with the "direct sibling selector", `+` – chazsolo Aug 16 '18 at 19:06