Select all elements except a div and its contents [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
jQuery: Target all except ___?
I've got a script that detects clicks on every element on the page. I'd like that to run on everything except a specific div and its children.
I've tried doing this using the :not selector, but don't really understand how to use that effectively, so I'm not sure if that's the right way to go or not.
Here's some sample code:
<div id="clickme">This is good stuff</div>
<div id="dontclickme">This should not get selected</div>
I'm currently using $("*") for my selector. Is there a way to use that to detect if it's the second div, or any of its children?

You can use .filter() with .closest() to check if element has a certain id or element is a children/descendant of element with certain id...
$('body *').filter(function(i, v) {
return $(v).closest('#dontclickme').length == 0;
})
http://jsfiddle.net/wirey00/7JE7x/

Select all divs, but not .class:
$("div").not(".class")
Works with other selectors too.

Related

Javascript: Css-Selectors: (Bug or Feature?) Class-Name "starts-with" only selects element, when class is at first position in class-list [duplicate]

This question already has an answer here:
jQuery: Finding partial class name [duplicate]
(1 answer)
Closed 2 years ago.
I want to select an element using querySelector(...)
Let us assume to have a html-fragment like:
<div class="Class1_a123 Class2_z987">Div1 content</div>
<div class="Class1_a123">Div2 content</div>
I want to select only the first div, if it (and this is set dynamically) has the second class.
So I wrote the selector as follows:
document.querySelector("div[class^='Class2']");
The result: Nothing is selected.
So I wonder, whether there is a hierarchy, which class stands in front of the class list.
I thought it checks for all contained classes, whether they start with the expression I need.
Is there a fundamental mistake in my mind and/or understanding, how selectors are working?
If this is right, that the order of classes inside an elements classlist can be important for the layouting/styles of the web-site?
In this case I have to think about all code I've written in past, where I used the class^='...' selectors and have it to replace by class*='...' to let it work as expected.
Any suggestions/corrections are welcome.
So basically, you are trying to query element which has attribute "class" starts with "Class2". To make it works, you need to change the order of the classes.
<div class="Class2_z987 Class1_a123">Div1 content</div>
<div class="Class1_a123">Div2 content</div>

Selecting a container DIV from a known link inside [duplicate]

This question already has answers here:
Getting the parent div of element
(7 answers)
Find the closest ancestor element that has a specific class
(6 answers)
Closed 3 years ago.
I am trying to work out how to select a container by a link contained within it. It looks something like this:
<div>
<p>Some text here</p>
My Link
</div>
I know the value of "My link". However, the div has no ID or class and is not contained within anything except the body.
How could I use javascript to select the div element so I can manipulate it? I've found ways to select the link using the DOM but not the outer div. There are many divs on the page, so I cannot just select div.
Your help is appreciated!
If the only thing you know is that it's a a as a direct child of a div and it contains the text My Link, you can find it by finding all div > a elements and picking the first one that has that text:
const a = [...document.querySelectorAll("div > a")].find(a => a.innerText === "My Link");
(That relies on the fact that NodeList become iterable a couple of years ago; you may need to polyfill that.)
a will now be the first matching a element, or null if none matched.
From there, if you want that element's container, you can use closest with a CSS selector. For instance:
const x = div.closest("CSS selector here");
...or just use parentNode/parentElement as needed.

Clear all the contet in class elements using getElementsByClassName [duplicate]

This question already has answers here:
.getElementByClassName not working? [duplicate]
(2 answers)
Closed 7 years ago.
Im trying to make all my div class elements to lose all its content by replacing their current content with nothing "".
document.getElementsByClassName("sprint_column").innerHTML = "";
But nothing happens with the "sprint_column" when im trying to reach the class, However if i try to reach the IDs it works:
document.getElementsById("div3_Score").innerHTML = "";
Here is the code where the Ids and Classes is created:
<div class='sprint_column' id='div3_".$team."'>Sprint 1</div>
Is there any way to clear all content from a class with "getElementsByClassName" or do i have to loop through all ID elements and clear them one by one?
getElementsByClassName returns an array of DOM elements. You need to iterate through them, e.g. via a for loop, and change the innerHTML one by one.

jQuery .each(), .find() and .append() only working on the last node? [duplicate]

This question already has answers here:
How to append one jQuery element already in the DOM to another element?
(5 answers)
Closed 3 months ago.
I am writing a Greasemonkey userscript, with jQuery. The segment of code affects forum thread pages, and intends to append a button to the footer of every post.
Assume the button is already a jQuery element $button, this is the code I have:
$('table.posts').each(function() {
//get the name of the poster
profileName = $(this).find('.user').text();
//change the link relative to each poster
$button.attr('href', '/search?user=' + profileName);
//This is the problematic line:
$(this).find('div.postFooter').append($button);
});
I have tested the value of profileName using an alert, and it successfully iterates through and alerts the profile names, but it only appends the button to the last post's footer (with the correct link).
I've tried using a variety of different selectors and ways to traverse the DOM to the required element, but all methods have resulted in the same thing. I'm out of ideas.
Use clone:
$(this).find('div.postFooter').append($button.clone(true));
Everytime you are changing and appending the same $button element.

Get Index Question - JQuery [duplicate]

This question already has answers here:
Select element by index (multiple elements of same class)
(4 answers)
Closed 2 years ago.
Possible Duplicate:
Select element by index (multiple elements of same class)
Quick question, I'm targeting all the article elements in my html5 page using:
var articles = $("article");
What I want to do is target one of the article elements using an index only. I can't seem to get it to work, any ideas?:
articles[1].css("display", "none"); // <-- This won't work
The array is returning the DOM element rather than the jQuery object. The .css() function does not exist on the DOM element, so you can wrap it in with the jQuery $ function to create a jQuery object that you can call .css() on.
Try $(articles[1]).css("display", "none");
Demo
Edit: Or even better articles.eq(1).hide();
You can use the .eq() function to target a specific index,
$("article").eq(1).css("display", "none");
According to the jQuery documentation referenced above,
Reduce the set of matched elements to
the one at the specified index.
Try this. This should target the first article
var articles = $('article').eq(0);
articles.css({"display":"none"});
Check this out for more of an explanation but this does exactly what you need.
http://api.jquery.com/eq/

Categories