jQuery Sibling Selector
jQuery sibling selector $("A + B") is used to select and element B that just comess after A. We just add a plus sign between two selectors (which can be any type of selector: IDs, classes, or elements). The selector on the right is the one to select, but only if it comes directly after the selector on the left
Take a look at the following example, where we have two tables and three links in the html.
|
You may like to try this example here.
Notice that the selector
$("table+a")
It select the the element a which is sibling of the table. So how it is different from selecting just the
$("a")
The $("a") will selet all occurrences of the a element and will make all the links vanish. Try to make this change in the online program above and see the result.
Exercise 1
Write the code that will vanish the two tables in the above example on clicking the Button. Check your answer here.
Exercise 2
In the above example, look at the line 8
$("table+a").hide();
If this is replaced by
$("p+a").hide();
what will happen if the button in clicked. Make a guess and check it here.