I am learning jQuery and a few of the problems in my class have been having me do selector practice.
This is probably hella basic, but how do I know if that specific HTML or CSS element I'm selecting requires it to be $('#element') or $('.element') instead of just $('element')?
jQuery (and the DOM functions document.querySelector and document.querySelectorAll) are using CSS selectors to select elements from the DOM.
So basically you look at (an) HTML element(s) and try to find something that identifies the element(s), for instance if you want one particular button:
<button id="login">Login</button>
You can use its id. IDs are by definition unique on the page, so you'll always get that button (or nothing if the ID is different or the button doesn't exist). The "id" selector is a # in front of the actual id, so you'd use #login here.
But what if you want to select multiple items that are similar? For instance:
<li class="menu-item">Home</li>
<li class="menu-item">Shop</li>
<li class="menu-button">Logout</li>
What if you want the all the menu-item elements, but not the menu-button?
This time you can use their class attribute. The class selector is prefixed with a ., so you'd use .menu-item to select those two elements.
There are many other elements and CSS has quite a bit of syntax. Here are a few examples:
You can select by the element's tagname: button selects all <button> elements
You can select by any attribute: button[disabled] selects all buttons with a disabled attribute like <button disabled>n/a</button>
You can select based on their parent: li img selects all the images that are directly embedded in a <li> element
I highly recommend reading the MDN article for more information.
I am trying to create collapsible tree. I this my leaf nodes have class="member". The code highlights the path user is on in the tree. The problem is when i am using $(this).toggleClass("xyz"); it is removing my "member" class.
I want to keep both class on leaf node
<li class="member xyz">Name</li>
jsfiddle code
using the member class I have css style that changes bullet images on my original code. so, i need to keep the member class.
Examine this code:
$("li.clickedli").removeAttr("class");
This removes the entire class attribute, thus removing all classes. Just set/clear the specific classes you need/don't need.
I am using D3 and I want to select all elements on the page that have a certain class. I have tried:
d3.selectAll("body").attr("body", "symbol-clicked");
but this assigns the class symbol-clicked to all elements on the page. I just want a collection of group of elements that already have the symbol-clicked class so I can change it to just symbol.
Any help is greatly appreciated.
Use
d3.select("body").selectAll(".className")
This will give you all the elements with the class 'className'.
To get elements with multiple classes. Try
d3.select("body").selectAll(".className1").filter(".className2")
So this is what's up:
onmouseover="imageOn(bg-index);" onmouseout="imageOff(bg-index);"
Those are two attributes I have on a table with ID table-title. The functions are a part of a remote JS file:
if (name == 'bg-index') { document.getElementById("table-title").style.backgroundImage = "url('images/bg-index.png')"; }
...with imageOff being the same thing but with a different image. This doesn't work; what am I doing wrong?
onmouseover="imageOn('bg-index');" onmouseout="imageOff('bg-index');"
Try it with the function variables as strings.
CSS has a specific hierarchy. Did you check if there are any other CSS entries that overwrite what you are trying to change? Perhaps there is some selector that is forcing that image as "always on".
There are four distinct categories which define the specificity level of a given selector:
Inline styles (Presence of style in document).
An inline style lives within your XHTML document. It is attached directly to the element to be styled.
E.g. <h1 style="color: #fff;">
IDs (# of ID selectors)
ID is an identifier for your page elements, such as #div.
Classes, attributes and pseudo-classes (# of class selectors).
This group includes .classes, [attributes] and pseudo-classes such as :hover, :focus etc.
Elements and pseudo-elements (# of Element (type) selectors).
Including for instance :before and :after.
I'm a little confused about HTML classes and IDs, as I'd like to use them BOTH to describe an HTML element. Is this valid and supported by most browsers?
The motivation for me to use both is this:
I have a CSS style that I would like applied to multiple elements.
I have some AJAX and Javascript that will manipulate those same elements, so I need a way to identify which element is which using an ID.
So I'd like to use an id to identify each element for JS manipulation AND at the same time I would like to specify a class so that the same style is applied from the same css.
An ID would be unique to only one item, where a class can be used to group many items together. You can use them together as you stated, ID as a unique identifier for Javascript and the class to markup with CSS.
Search for html class vs id to get many articles about this topic.
Example:
<ul>
<li class="odd" id="item1">First Item in the List</li>
<li class="even" id="item2">Second Item in the List</li>
<li class="odd" id="item3">Third Item in the List</li>
</ul>
Yes, it is perfectly valid to use both the ID and Class properties in one element. Example:
<div class="infoBox" id="myUniqueStyle"> *content* </div>
Still, keep in mind that an ID can only be used once (hence its name), while you can use classes as many times as you'd like througout a document. You can still use both the ID and the class to apply styles, while only the ID is a simple way of reaching the element through javascript.
A good way of doing it is applying IDs to all elements that you know are unique (header, navigation, main containers etc.), and classes to everything else.
"Is the" applies to elements using ID: "This is the navigation bar", "this is the header"
"Is a" or "is an" applies to elements using classes: "This is a blogPost", "this is an infoBox" etc.
You can definitely use both if you need to.
An ID is typically used to identify structural sections of your site - you should have only one element with a particular ID and any element can have only one ID.
A class is used to set styles which might be used in more than one place in your HTML file - any element can have multiple classes set.
A typical HTML document using both IDs and classes might be something like
<html>
...
<body>
<div id="header"></div>
<ul id="nav" class="full-width dark">...</ul>
<div id="content">
<div id="important-container" class="class-set-by-javascript another-class-set-by-javascript"></div>
</div>
</body>
</html>
Yes, any normal browser should allow the setting of CSS classes regardless of element id. However, setting styles on a specific element (using ids, for example) may override styles set through a CSS class.
Just a very obscure note about combining class and id in your CSS declarations, there's a bug with IE6, if you have:
two or more pages which have an
element with the same id
those elements have different
classes
you're styling them using an
#idname.classname rule
then only the first rule in the stylesheet will take effect.
See this page for details
Yes it is valid in all browsers. ID expresses just the unique IDentification of your html control through others, and class applies some style to it. Use IDs when there is only one occurence per page. Use classes when there are one or more occurences per page.