Filtering elements upon loading

I have successfully implemented data filtering on my website using data-filter.

While everything is working great, I am interested in displaying items with specific attributes upon loading.

For instance, I want to load only green objects by default instead of displaying all items.

http://jsfiddle.net/joshvogt/UybPY/

HTML:

<section>
  <a href="#" data-filter="all" tabindex="-1">ALL</a>
  <a href="#" data-filter="red" tabindex="-1">RED</a>
  <a href="#" data-filter="green" tabindex="-1">GREEN</a>
  <a href="#" data-filter="blue" tabindex="-1">BLUE</a>

<div data-filter="red"></div>
<div data-filter="blue"></div>
<div data-filter="red"></div>
<div data-filter="blue"></div>
<div data-filter="green"></div>
<div data-filter="red"></div>
<div data-filter="red"></div>
<div data-filter="red"></div>
<div data-filter="blue"></div>
<div data-filter="green"></div>
<div data-filter="blue"></div>
<div data-filter="green"></div>
<div data-filter="green"></div>
</section>

CSS:

section {
  display: block;
  float: left;
  border: 2px solid green;
  min-height: 300px;
  width: 100%;
  border-radius: 4px;
}

a {
  display: block;
  float: left;
  width: 25%;
  text-decoration: none;
  text-align: center;
  padding: 5px 0;
  color: white;
  background: #1271C7;
}

div {
 display: block;
 float: left;
 height: 40px;
 width: 40px;
}

How can I achieve the display of only green elements upon loading?

div[data-filter="green"]{
  background: green;
}

Answer №1

After carefully reviewing your request, it seems that this solution might be what you are looking for:

.wrapper {
  display: flex;
}

.box {
  width: 100px;
  height: 100px;
  margin: 10px;
  background-color: #f0f0f0;
}
<div class="wrapper">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</div>

Answer №2

Initially hide all and display only items in green:

div[data-filter] {
    opacity: 0;
    visibility: hidden;
}
div[data-filter="green"] {
    opacity: 1;
    visibility: visible;
}

See the live demonstration here: http://example.com

Answer №4

Once the document is ready, use the following code:

$(document).ready(function(){
    $('section div:not([data-filter="green"])').hide();
});

Upon loading, it will search for div elements with the "data-filter" attribute set to green and hide the rest.

This approach is suitable for the task at hand.

Answer №5

Give it a shot....

$("section[data-type!='red']").each(function (index, element) {
     //This function will hide the selected elements
    $(this).hide();
 });

Check out the demonstration here:http://jsfiddle.net/AbCdE/123/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Overlay a div on top of an image in an HTML email

To display text over an image in HTML email, I am looking for a solution since using margin or position is not working as required. Below is the code snippet I have tried: <div style="padding-right:25px;padding-left:20px"> <div style="padding-t ...

"Unraveling the Mystery of jQuery In

I am facing an issue with integrating jQuery into my code. I have always followed the same method, which has worked in the past, but now it seems to be causing problems for me. I suspect it may be because I am not on my usual laptop, although when I visite ...

Creating a stylish button using a combination of CSS and Javascript classes within a webpage layout

Is it feasible to include a button in the layout that has HTML, CSS styles, and JavaScript functionality? For instance: This button is designed with both CSS styling and JavaScript classes. How can one incorporate CSS and JavaScript along with HTML conte ...

Using dots instead of lines for the carousel indicators in PrimeNG

I'm currently working on a carousel feature, but I want to change the indicators from lines to small dots. I know the solution lies within the CSS files, but I'm not sure how to implement it. I think I need to create a new CSS class, but I could ...

"Learn how to properly load the style.css file from the root directory into your WordPress page.php and single.php files

Having an issue with my WordPress theme. The style.css stylesheet does not seem to be loading in the page.php contents. I noticed that when I add <div class=w3-red"> <button class="w3-btn w3-red"> and other related codes while editing a post in ...

"Searching for the index of a clicked element? Here's how to do

I am trying to determine the index of a clicked anchor element in my navigation using jQuery. However, when I use the following code snippet, I always get zero in the console: $('.navigation ul li a').click(function(e) { e.preventDefault(); ...

Restrict the amount of pages displayed in pagination

Currently, I am developing a jQuery pagination tool. While the pagination functionality is working smoothly, I have identified a drawback: On desktop devices, showcasing more than 14 pagination links in a row is acceptable. However, displaying such an ext ...

trouble encountered when attempting to integrate typeahead functionality in AngularJS using jQuery

Greetings! I am brand new to using AngularJS and currently exploring the implementation of typeahead functionality. I decided to utilize an existing library by including the following script: <script src="lib/xyz/typeahead.bundle.js"></script> ...

Adjust the text color of a div element by clicking a button with the help of JavaScript

I am trying to change the font color of my div when a button is clicked using JavaScript. The two buttons I have are 'Green' and 'Red'. <button type="button" class="green">Green</button> <button type="button" class="red" ...

Using Python and Selenium to Scroll Down the Page (with Two Separate Scrollbars)

I need help scrolling down the conversation section on Facebook Messenger. There are 2 scroll bars on the page, and I specifically want to scroll down scroll bar number 1 (see image) Click this link to access the page (make sure you are logged in and hav ...

changing the value of a text input based on the selected option in a dropdown using ajax

I attempted to update the text input value with data from the database after selecting an option from the dropdown list. However, I encountered an issue where the data is not populating the text input field. <?php //Including the database configuration ...

Uncovering the media:thumbnail in an RSS feed using Scrapy's CSS selector

I am currently working on parsing an RSS feed that includes image URLs. The XML file structure is as follows: <item> <title>Item Title Goes Here</title> <link>https://www.google.com</link> <media:thumbnail url="https:/ ...

Display or conceal form elements depending on the JSON response

Upon making an api call, a json Response is received with the following structure: {"Field":"Amount","FieldName":"Amount","FieldType":"Numeric","MaximumLength":128,"MinimumLength":0,"Options":"Mandatory"} This api call yields between 5-10 of these object ...

Discovering JSON data embedded within a script tag in an HTML response using Jsoup

Looking for help with extracting JSON content from script tags embedded in an HTML file. I attempted to use Jsoup without success. Can anyone provide guidance on utilizing JSOUP or another library for this task? Thank you in advance. ...

Using window.print as a direct jQuery callback is considered an illegal invocation

Curious about the behavior when using Chrome $(selector).click(window.print) results in an 'illegal invocation' error $(selector).click(function() { window.print(); }), on the other hand, works without any issues To see a demo, visit http://js ...

Learn how to reposition the mat-option easily

I have an angular app with an autocomplete field that I need to adjust the position of. I have consulted the official documentation under the method updatePosition, which states: "Updates the position of the autocomplete suggestion panel to ensure that it ...

Creating a button in HTML that performs a POST request without redirecting involves using specific code techniques

Looking for a way to create an HTML button that triggers a POST request to a quick route with parameters passed through req.params. The challenge is preventing the button from redirecting me to the route when clicked, but instead staying on the same page w ...

Error encountered in jQuery's addClass and removeClass functions: Unable to read the property 'length' of an undefined value

Upon loading my page, I aim to have some of the div elements hidden initially and display only one. Here is a script that accomplishes this goal: <script> $(document).ready(function () { $(".total").click(function () { $("#pi ...

Prevent loss of data when user incorrectly submits a webform

Currently, I am working with Wordpress and a contact form plugin which is based on php. The form provided by the plugin is quite lengthy and my client has requested that the data entered by users should not disappear if they make a mistake and need to co ...

What is the best way to add color to a Table Header?

I'm working on a HTML/CSS table and I want the header row to have a specific background color. Below is my code: <table class="contentTable"> <tr class="contentTableHeader"> <th>No.< ...