Searching for a class and implementing a style using jQuery: a beginner's guide

i found this neat example on jsfiddle of an accordion created using only css3, no javascript involved.

HTML:

<div class="ac-container">
    <div>
        <input id="ac-1" name="accordion-1" type="radio" checked />
        <label for="ac-1">About us</label>
        <article class="ac-small">
            <p id="test_small">test</p> <-- content loaded from php
        </article>
    </div>
    <div>
        <input id="ac-2" name="accordion-1" type="radio" />
        <label for="ac-2">How we work</label>
        <article class="ac-medium">
            <p id="test_medium">test</p>
        </article>
    </div>
</div>​

CSS:

.ac-container input:checked ~ article.ac-small{
    height: 140px;
}
.ac-container input:checked ~ article.ac-medium{
    height: 180px;
}
.ac-container input:checked ~ article.ac-large{
   /* height: 240px;*/
}

I'm facing an issue with the fixed height set in CSS and dynamic content loaded via PHP. Since I am unaware of the content's height beforehand, I need a way to adjust the height dynamically.

By loading content into the p tag through PHP, I can calculate its height using:

var v = $("#test_small").height();

// Add the calculated height to .ac-small element
$('.ac-container input:checked article.ac-small').css("height", v+'px');

The problem lies in applying CSS to the selector properly, as it seems like there might be an issue with the selector

$('.ac-container input:checked article.ac-small')
.

Answer №1

attempt

$('.ac-container input:checked').parent('div').children('article.ac-small').css("height", v+'px');

Answer №2

Your CSS selector may not be working as expected. Let me clarify what that selector is targeting:

$('.ac-container input:checked article.ac-small')

This will target the <article> elements with the class ac-small that are inside a checked input within an element with the class ac-container.

However, it's not valid to have an element inside of an input...

Your CSS selector looks correct though:

.ac-container input:checked ~ article.ac-small{
    height: 140px;
}

Answer №3

Initially:
The first step is to modify the identifier to a category

var h = $(".ac-small").height();

Following:
In case you wish to access the adjacent element of your input, follow these steps:
Locate the selected input within the container, search for its article sibling, and adjust the css accordingly.

$('.ac-container').find('input:checked').siblings('article').css("height", h+'px');

Answer №4

If you want to target the next sibling element using jQuery, you can use the next sibling selector. The `~` symbol selects the next sibling that matches the specified selector, rather than selecting a child element:

$('.ac-container input:checked ~ article.ac-small')

Alternatively, you could achieve the same result by writing:

$('.ac-container').find('input:checked').siblings('article.ac-small')

To be more precise and select only the next sibling element, you can also use:

$('.ac-container').find('input:checked').nextAll('article.ac-small')

Answer №5

This is the way to proceed

let heightValue = $("#small-screen").width();

However, there is no element in your HTML with an id of "small-screen".

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 React style background property for CSS styling

Is there a way to apply the css property background inline in react? I attempted to pass the following object without success: let style = { background: `linear-gradient( rgba(0, 0, 0, 0.6), rgba(0, 0, 0, 0.6) ...

Having trouble getting the `nth-of-type` and `nth-child` selectors in CSS to function

I am attempting to apply a green color for the first nth child flag element <div id="axis"> <div class="super"></div> <div class="flag">flag 1</div><!-- I want this text to be green--> <div class="supe ...

The function is missing from the object, leading to a script error with jQuery

When using different versions of jQuery, I encountered some issues with saving changes in my application. Initially, with jquery-1.4.4.min.js, everything worked except for the save function due to an error I made. However, when switching to jquery-1.7.1.mi ...

Tips for maintaining tooltip styles following an Ajax request

I'm currently working on styling tooltips within my datagrid and I've encountered an issue when moving to the next page after making an Ajax call. Here is the basic code snippet that I have been using. I believe the solution involves using "on", ...

What is the process for importing jQuery UI with ES6/ES7 syntax?

Struggling to integrate jQuery UI functionality into my reactJS/Redux application. I have installed both jQuery and jQuery UI using: npm install jquery jquery-ui Then, attempted the following imports: import $ from 'jquery'; import jQuery from ...

How can we use jQuery to replace a u002D in a string obtained from an attribute

My goal is to extract the URL inside the "ng-init" attribute from this particular div element. <div ng-controller="CloudcastHeaderCtrl" ng-init="juno.replaceTracklist=true;juno.guid='DBFFF66E\u002D4111\u002D446B\u002D8471\u002D ...

Is there a way to roll up the background image of the body?

body { background: url(http://andreypokrovskiy.com/image_hosting/boredom/space-bg.jpg) repeat; animation: backdrop_roll linear 100s infinite; } .enemy-animation { position: relative; z-index: 1; animation-direction: alternate; animation-durati ...

Ways to prevent an element from displaying a hover border or outline

I need help with styling a group of 50% width elements that are displayed next to each other. I want to add a 20px white border around each element to create a separation between them. This is necessary because I have a responsive layout and always require ...

Fuzzy text in drop-down box on Chrome, clear on Firefox

I've encountered an issue with a dropdown menu in Google Chrome where the content appears blurry, while it displays correctly in Firefox. The problem arises when the dropdown exceeds a certain height, and I've tried setting a max-height with over ...

The body background position center does not function properly in Internet Explorer

I'm currently working on a website and facing some issues with the background-position property in CSS. Here is the code snippet that's giving me trouble: body { background-color: Black; background-image: url(images/background_ui.png); backg ...

Organize elements with precision using html and css properties

I am struggling to get the buttons on the card to align properly. I have set a minimum height to ensure they are consistent in size regardless of content, but the alignment of the buttons is off. Take a look at the photo attached for reference - I want the ...

Renewing Masonry and JQuery

Currently implementing Masonry from into a project aimed at creating a timeline effect for user interaction. The goal is to automatically refresh the page at intervals (every 10 seconds during testing), but encountered an issue. Upon initial load, the ge ...

Passing an object in an ajax call to a function: a comprehensive guide

@model IEnumerable<HitecPoint.BlackBox.Models.SMSReportModal> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"> </script> <script type="text/javascript"> var MyAppUrlSettin ...

``I'm having trouble with TablePagination styling on a material-table component. Anyone have

I am trying to customize the appearance of rows numbered from-to of x on a Material-Table, using the following code: import MaterialTable from 'material-table' import { TablePagination, withStyles } from '@material-ui/core' const Style ...

Setting the default date in JQuery datepicker and using multiple datepickers

Currently, I am utilizing a datepicker tool which can be found at this link. In the HTML form that I have, there are two date fields. My goal is to have the first field default to today's date and the second field default to today's date minus 7 ...

When a user clicks on buttons other than the submit button, the input field redirects to another page

A helpful user named Vittore assisted me in displaying the first 5 list items of the unordered list and implementing page navigation with next and previous buttons flawlessly. The issue arises with an input field, which functions correctly by navigating ...

Bootstrap Modal Form Successfully Saves All Except Select Dropdown Choices

I am facing an issue with my modal window where all data is being saved except for the select form: <div class="form-group"> <label for="tax_code_modal">Tax Code</label> <select value="" name="t ...

Javascript syntax error: Unexpected ending of data while trying to parse JSON data at line 1, column 1

I operate a CS:GO betting platform, and I encountered an issue when attempting to access the page for withdrawing skins. After completing the reCAPTCHA verification process to confirm that I am not a robot, I received the following error: Javascript err ...

challenge with altering data in transmission when using the GET method

$('.textedit').editable('/webpage/skeleton/edit_text/text/process', { loadurl: '/webpage/skeleton/edit_text/loadraw', loaddata: {id: $(this).attr('id'), client: $(this).data('client')}, submitda ...

Place the menu within the image, positioned at the bottom

Could someone help me with placing a menu at the bottom of an image using HTML, CSS and Bootstrap 4? I have limited CSS knowledge and need assistance. Currently, my navbar is located below the image but not within it like this: I am looking to achieve so ...