Verify if the second list item contains the "current" class

When displaying a list of blog items on the blog page, I am using grid-column: 1 / -2;. In the first row, the first blog item spans two columns while the second and subsequent rows display three items in each row. This setup works well, but when moving to the second or third page, the first row remains the same. I only want the latest blog to stretch across two columns in the first row, with three columns displayed in the first row on all other pages.

To work around this issue, I found a solution in the pager control by checking if the class current is present in the second element.

My question now is, how can I use jQuery to detect if the UL LI element has the class current in the second position? If it does, I want to change the grid-column: 1; to display three columns in the first row on the 2nd, 3rd, 4th, or 5th pages.

https://i.stack.imgur.com/aeI6Q.png

The code for my pager control is as follows:

<ul id="ContentPlaceHolder1_PagerControl1_pnlPager" class="nav-pager">
<li><a id="ContentPlaceHolder1_PagerControl1_-1" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$PagerControl1$-1','')"> « </a></li>
<li><a id="ContentPlaceHolder1_PagerControl1_0" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$PagerControl1$0','')">1</a></li>
<li class="current"><span id="ContentPlaceHolder1_PagerControl1_39e9e1f727784cce80f1a3b5e6e43ac0">2</span></li>
<li><a id="ContentPlaceHolder1_PagerControl1_2" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$PagerControl1$2','')">3</a></li>
<li><a id="ContentPlaceHolder1_PagerControl1_26" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$PagerControl1$26','')"> » </a></li>
</ul>

I attempted the following code for testing purposes to see if it would work. This simple code, where I try to change the background color, did not work:

if($("ul li").hasClass('current')) {
$(this).css("background-color", "yellow");
}

Answer №1

Resolved: I utilized jQuery to showcase two items in the first row of the initial page, and three rows on all pages.

Cascading Style Sheets (CSS):

.article:nth-child(1) {
   grid-column: 1;
}

jQuery Section

$(document).ready(function () {
        var counter = 1;
        $('.nav-pager li').each(function () {
            if ($(this).hasClass("current")) {
                if (counter == 2) {
                    //  alert("Found : " + counter);
                    $(".article:nth-child(1)").css("grid-column", "1 / -2");
                  
                }
            }
            counter = counter + 1;
        });
    });

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

A pair of jQuery UI datepickers each configured with distinct settings

Check out this example here I am setting up 2 datepickers. <input type="text" class="datepicker" id="date_first_registration"> <input type="text" class="datepicker" id="inspection_valid_until"> For the first one, I want the date range to sta ...

Fixed position of the rotated button placed at a 90-degree angle on the right side of the screen

Recently, I had a project where I had to include a div that was rotated 90 degrees and fixed to the right side of the screen. The challenge was making sure that as you scrolled down, the button moved along with the screen while maintaining responsiveness. ...

Serializing ajax calls using `WHEN` and `DONE` in jQuery

I have several ajax methods that need to be executed, and I want to run some code after all of them have successfully completed. I am unable to modify or redefine the ajax methods. Can you please advise me on how to achieve this? I attempted to use WHEN b ...

The background image is failing to display, although other styles for the class or element are visible

Why won't the background image display, even though I have verified that the image path is correct and changing other properties like color or background color works just fine? Here is my CSS code snippet: .container { background-imag ...

Storing the current URL in the controller prior to leaving the page

Due to certain requirements, I need to save the URL of the page that the user is leaving in order to use it in other views and controllers. For example, if the user is currently on www.page1.com and is navigating to www.page2.com, I want to somehow save th ...

What is the best way to convert JSON data into an object instance using Bridge.NET?

Currently, I am developing a project using Bridge.NET that dynamically generates Bootstrap forms based on template data received as JSON through a jQuery.Ajax call. The issue arises when attempting to convert the JSON data back into their object represent ...

What is the computed value of HTML and body?

Is there a specific reason why the body tag has a default margin while HTML does not? Could there be any practical explanation for this design choice? ...

Expanding using CSS3 to ensure it doesn't take up previous space

Currently, I am working on an animation for my web application. More specifically, I am looking to scale certain elements using CSS3 with the scaleY(0.5) property. These elements are arranged in a vertical list, and I want to ensure that they do not take u ...

Merging JavaScript with Less

As I attempt to extract the height of a div using JQuery and style it with less, I am facing an issue. Despite placing my JavaScript at the bottom of the page and less at the head, the compilation of less appears to occur after my JavaScript. Upon attempt ...

Guide to transferring an ID received via JSON through Ajax to a PHP POST request

I am struggling with an AJAX function that displays an HTML table. The issue I'm facing is that I have a method in viewmap.php that shows the location based on the ID you click on the table. However, to make this work, I need to send the UID to viewma ...

Learning to master each individual jQuery method is an essential skill for any web developer

I'm working with the following function but $('input[id^="ProductId_"]').each(function () { it's giving me a different output than what I need. The desired result should be obtained from $('input[id^="ProductId_"]').cli ...

How do you position items in the center and lower right corner of a flexbox that takes up the entire

One feature on my website is a flex box that takes up the entire window with a width of 100% and a height of 100vh. Within this flex box, I've centered text and a button, but now I'd like to add a footer to the page. If I simply place the footer ...

What is the best way to eliminate the Iframe scrollbar while ensuring that the entire page loads?

When I add an Iframe inside the contentArea, two scroll bars appear. I am looking for a way to hide the iframe scrollbar without hiding any of the external website's content. I have tried using the scrollbar="no" snippet code, but it didn&ap ...

Creating an AJAX form using Jquery 3.3.1: A step-by-step guide

After updating my jQuery from version 1.10.2 to 3.3.1, I encountered a problem. Whenever I fill in the inputs and click the send button, my website simply reloads and the information from the inputs gets added to the URL of my website. The URL looks like t ...

default browser's drag and reposition feature

I'm trying to make an image draggable on my webpage by using default browser behavior, but it's not working for some reason. Here is the code snippet I'm using: <div style="position:relative;width:1000px; height:900px;border:solid;z-inde ...

Achieving a responsive card layout in Reactjs with Bootstrap by displaying four cards in a single row

const DisplayCategory = () => { const history = useHistory(); useEffect(() => { axios.get('http://localhost:8080/products', { headers: { Authorization: "Bearer " + localStorage.getItem("token&q ...

retrieve the class name of a reusable component in HTML

I have been assigned a web scraping project and I am required to scrape data from the following website for testing: The HTML: <div class="quote" itemscope itemtype="http://schema.org/CreativeWork"> <span class="text& ...

Utilizing background images in CSS to enhance bootstrap icons

Here is the HTML code snippet I'm working with: <a class="iconContainer" href="#"> <span class="containerIcon chevron-right"/> Test </a> And this is the corresponding CSS code: .chevron-right::befor ...

How to Submit an Iframe Form Using Jquery and Retrieve the Response

I am currently working on a webpage that contains an iframe with a form inside. My goal is to submit the form and retrieve the result using jQuery. What would be the best way to accomplish this? <iframe src ="html_intro.asp" width="100%" height="300" ...

Exploring the Power of jQuery Ajax Requests

Currently, I am making an Ajax call from site.com/users/{username} I need to access the url site.com/account/deleteComment, but when I check in fireBug it seems to be trying to access site.com/users/account/deleteComment Below is the code snippet: $ ...