using generated divs to target a pseudoclass

I have designed a webpage that utilizes the CSS :target selector to determine which tab of information should be displayed. These tabs are generated dynamically through AJAX-loaded data when the page is initially loaded. Below is a snippet of code showcasing how this functionality works:

<!DOCTYPE html>
<html lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
    </script>
<style>
    #store div.tabs > div > a { background: #900;}
    #store div.tabs > div:target > a { background:#090;}
</style>
    <title>Test</title>
</head>
<body>
<script type="text/javascript">
function setStore() {
    var lastCategory = "";
    for (var i=0;  i<storeList.length; i++) {
        if (storeList[i].category != lastCategory) {
            lastCategory = storeList[i].category;
            $("#display").append('<div id="'+lastCategory+
                                    '"><a href="#'+lastCategory+'">'+lastCategory+
                '</a><div class="content"></div></div>');
        }
    }
}
function getStore() {
    function startWithStore(jcal) {
        storeList = JSON.parse(jcal);
        setStore();
    }
    $.ajax({
            url: 'getStore.php',
            type: "POST",
            success: startWithStore
    });
}
$(function() {
    getStore('display');
});
</script>

<div id='store'>
<div class="tabs" id='display'></div>
</div>
</body>
</html>

You can view this page at

Upon initial loading of the page (example, "testtarget.htm#Linen"), the Linen div is not selected automatically. However, clicking on any of the links functions correctly to select the respective div. It seems like the target property is not being applied to the generated div.

Hence, I am seeking the most effective way (whether pure HTML/CSS or using jQuery) to ensure that the target div specified in the initial URL is selected upon the page's first load.

Answer №1

$(function() {
    $('.mylinks:first-child').addClass('makemelookgreen');
});

When the document is fully loaded, a specific element with a class of "mylinks" that is the first child will have the class "makemelookgreen" added to it using jQuery's .addClass() method.

In addition, if the hash at the end of the URL is not activating the link as expected, it could be because elements do not receive the ':active' pseudo-class unless a click event is triggered. The hash in the URL simply directs the page where to scroll to, rather than activating any specific functionality.

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

The image in the top left corner is not aligned properly with the menu

How can I ensure that the top left image (logo2.jpg) on my page stays aligned with the menu below it instead of shifting to the right? Here is the link to the page for reference: #logo { margin:0 auto; width: 975px; position:relative; } #top { position ...

Chrome distorts the display:table when zoomed in

Consider this CSS snippet: .table { display:table; } .table-row { display: table-row; } .table-cell { display: table-cell; } Now, let's add some HTML to go along with it: <div class="table"> <div class="table-row"> ...

Is there a way to include the request body (req.body) in the msg object using express-winston?

My current challenge involves logging {{ req.body }} using the msg: object in express-winston. Even after whitelisting the body with expressWinston.requestWhitelist.push('body');, it still does not appear in the log. export const accessLogger = ...

Difficulty in aligning tables with absolute positioning in Internet Explorer compared to Chrome

Encountering an issue with the alignment of elements when using position: absolute; across multiple browsers. In Chrome, everything appears properly aligned within the table. Refer to the following link for a visual: https://i.sstatic.net/qhrAi.jpg Howe ...

Is it possible that react-gtm-module is generating a faulty source path?

It seems that the react-gtm-module is generating an incorrect src path, substituting a "/" with a ".". This is the original src path provided by Google: . However, the generated src path by react-gtm-module is: gtag: . Google Tag Manager detects this i ...

The drop-down menu remains in the open position following a responsive test

Seeking assistance with a minor issue... I've incorporated a jQuery mobile menu into my responsive web design. When testing the responsiveness by adjusting the browser window size, everything seems to be working correctly. However, upon resizing it b ...

Submitting twice: Rails 4 form submitting twice via AJAX with both text and JS involved

It seems like I've tried everything, but the form keeps submitting twice. When I'm in the index directory, the first process goes smoothly but the second one triggers a rollback in the console; however, everything works fine. When I navigate to a ...

Enhancing Form Validation with Vuejs 2

With vue-validator being prepared for Vuejs 2, what is the most effective method for implementing frontend validation? UPDATE I've come across a fantastic plugin called Vee Validate ...

Best practice for entering events in callback

Recently, I delved into Angular because I anticipate needing to use it down the line. My current focus is on studying components communication, particularly from child to parent. I came across various methods of achieving this. In each example, the ChildC ...

Is there a way in Angular to separate and protect the service layer from other components?

Currently, I am utilizing AngularJs in the front-end of my project and interacting with a RESTful service. The process involves composing a JSON payload for sending requests and receiving responses in JSON format. In addition to this, there are Angular mo ...

Mysterious HTML/CSS element causing page to stretch beyond limits

I am currently updating a website to make it responsive. Check it out here: When the viewport is below 980px in width, a horizontal scroll bar appears and the html/body/container remains at 980px. I've attempted to apply an inline width of 200px to ...

Creating consistent web page designs: A guide

Having created multiple practice websites, I have successfully published one. However, I am now faced with the challenge of needing to make design changes. If I want to modify the text surrounding a certain element in every page, I find myself having to ...

Rearrange columns to optimize html email layout for mobile devices

I'm currently trying to figure out a way to display 3 columns in a table row, but hide either the first or last column depending on whether it's being viewed on mobile or desktop. Initially, I thought about adding an extra TD at the bottom and us ...

What is the best way to transfer a variable from one event handler to another using jQuery?

What is the best way to pass a variable from one event handler to another? $("#vaccines").find("a").click(function() { var selectedVaccine = $(this).attr('id'); }); $("#options").find("a").click(function() { var selectedButton = $(this) ...

Could we incorporate a backwards typewriter animation?

Is there a way to delay this animation for 2 seconds before playing in reverse? I came across this online and made some edits, but I'm still new to this (early stages). Can this be achieved using CSS, HTML, and JavaScript? I plan to use this as an ale ...

Avoiding unauthorized cookie access via browser console

Is it possible to prevent unauthorized access to the value of cookies using document.cookie from the browser's console? What security measures can be implemented to secure the cookies? https://i.sstatic.net/gUmSb.png ...

Manipulate the contents of an HTML class using Javascript to substitute text with an empty string

Is there a way to create a JavaScript function that can remove text upon clicking from multiple HTML fields with the same class? I am facing an issue where my function is not clearing the data in the target field when "Yes" is selected in the trigger. Re ...

Can I switch between different accounts on Auth0?

While utilizing next-auth for user sign-ins, I've noticed that there isn't a clearly visible option to switch accounts after signing in. Additionally, if users enter incorrect credentials, there doesn't seem to be a way to sign in with a dif ...

Is it possible to enqueue a handshake after already having enqueued one in Socket IO?

Encountered a 'cannot enqueue handshake after already enqueuing a handshake' error when attempting to establish a SQL connection in Node.js. Seeking assistance in resolving this issue. Below is the code snippet that I am working with: io.on(&a ...

What are the steps to effectively create a cascade of Q promises?

Consider the following scenario as an illustration: I have 3 URLs stored in an array called "urls" The "require" function returns a promise that performs an $http call The code provided is functional, but it doesn't meet my desired outcome since th ...