This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a CSS parent selector?
I am able to hide a DOM tree which look in this way by using .closest().
<div class='parent'>
<!-- some html code -->
<div class='child'>
</div>
<!-- some html code -->
<div>
$('.child').closest('parent').hide();
It will be possible to get the same effect just by using CSS?
If yes, how?
No selector currently exists that can select a previous or parent element.
There is a level 4 selector that is currently being developed.
So in the future, you may be able to do something like this:
!.parent > .child { display: none; }
But until then, you'll have to stick with
$('.child').parent();
in jQuery.
No. See for yourself, no such selector exists in CSS3, and not in CSS2 either.
Might be able to use this
.child:parent .parent{display:none;}
http://css-tricks.com/parent-selectors-in-css/
Related
This question already has answers here:
jQuery find which parent is closer?
(5 answers)
Closed 3 years ago.
I need to find the .closest() ancestor with any one of the given class.
jQuery("#gid").closest(".ui-tabs,.tab-pane,.jomsTabsContent");
What I want here, is which ever of the given classes is the closest one, I need to select that.
So for the following eg.
HTML
<div class="foo">
<div class="bar">
<div class="monkey">
<div id="gid">
<div>
<div>
<div>
<div>
JS
jQuery("#gid").closest(".foo,.bar,.monkey");
I want the above to select (only) the .monkey div.
PS: I know the above isn't correct syntax for what I'm 'looking for', but I am demonstrating what I am looking for.
Opps... It looks like, If there is an overlapping hirerchy, then then .closest() returns the closest of the provided CSS group.
Got it after a little google-foo here.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
If I have many following divs:
<div class="error"> </div>
<div class="error"> </div>
<div class="error"> </div>
<div class="error"> </div>
<div class="error"> </div>
...
What is the best way (and best practice) to hide them all at once?
$('.error').hide() or
.addClass('hide') including .hide { display: none; } ?
Best is an opinion, you can run jsperf tests and each browser will be different.
In the end you will either loop in JavaScript and add classes or set style attributes or just rely on the CSS to do the looping for you.
Look at using a selector
$(".error").hide(); // or .addClass() or .css("display","none")
Under the covers it does:
a DOM lookup for one or more elements
it is doing a for loop over the set
it gets the current element in the loop iteration
Applys a css rule in the loop iteration
Ends up doing a redraw/repaint
But one way to not have to loop is to just rely on adding a CSS rule higher up in the hierarchy.
CSS:
.hideErrors div.error {
display: none;
}
JavaScript:
$(document.body).addClass("hideErrors");
What this does:
DOM lookup for one element
Adds one class
Ends up doing a redraw/repaint
This way you do not have to loop through and add a class to every element. It would be better to place the "hideError" rule around the element that wraps the error list. So change "body" to that parent element.
It really doesn't matter which you pick. If it's that important to you, run a benchmark.
Most of the browser's time would be handling layout and repaint anyway, the addition of class or inline style (Because that's what .hide() does, add style="display: none; to the element) really doesn't matter either way.
If you care about performance, drop jQuery, start using some vanilla JS, learn about page performance and optimize your JavaScript and CSS selectors.
Honestly, how you modify the DOM is the last thing you should be worrying about :)
The term best isn't really enough information, because it can mean performance (speed) or size (included libraries).
If you already are using JQuery, it is generally a very efficient library and the following is how you would do that after the DOM finished loading.
$( document ).ready(function() {
$('.error').hide()
});
There are other events besides 'DOM ready' that JQuery defines that you can hook the action up to, which is why its a convenient library.
If you are just interested in overall speed and want to avoid external libraries, CSS is generally your fastest way to go, and you would add the class as you stated in your Style Sheet. This limits when you can hide them to before the DOM loads.
There are best and worst cases for all of the situations you described. Javascript will have to loop (parse) through the DOM so it will always be slower than pure CSS.
I think add and remove class will be better way to handle this.
In this way you can also do other styling changes related to error message.
css code :
.showdiv {
display : block;
}
.hidediv {
display:none;
}
javascript code :
$('.error').addClass('showdiv')
$('.error').removeClass('hidediv')
If you would like them hidden on page load add a class hidden to the divs.
.hidden{
display: none;
}
To show and hide just use jquery.
$('.error').hide();
$('.error').show();
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Change an element's CSS class with JavaScript
Found lot of topics about this topic, but none helped me. I have:
<div id="text-6" class="block col-228">
Javascript code should add new class fit, so result should be:
<div id="text-6" class="block col-228 fit">
How to do that with javascript?
Try like this:
var elem = document.getElementById("text-6");
elem.setAttribute("class", elem.getAttribute("class")+" fit");
Important note: You need to be assure,that your DOM Hierarchy is constructed completely,then try to manipulate DOM Elements. That is why you need to put DOM manipulation scripts inside of the window.onload callback, or if you use jQuery, then inside of $(document).ready callback.
You put the following between your script tags.
document.getElementById('text-6').className += " fit";
I'd recommend using jQuery. Then you can just do something like this:
$("#text-6").addClass("fit");
EDIT:
Including the whole jQuery library may be overkill for your situation, but if you're dealing with DOM manipulation a lot, jQuery can simplify things quite a bit. If it's just for this one thing, then some of the other answers provided would be better.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
nearest ancestor node in jQuery
html:
<div class="post">
<h2>Pellentesque</h2>
<p>More</p>
</div>
jQuery
$('.more').bind("hover", function(){
$(this).parent('.post').hide() ;
});
on hover (.more) i want to hide the post, but it does nothing
if i use parents() instead it deletes ALL the .posts on the page
any help appreciated
Try
$('.more').bind('hover',function()
{
$(this).closest('.post').hide();
});
here is the working demo with single class.
here is the demo with multiple classes.
Use jQuery.closest : http://api.jquery.com/closest/