Updating an attribute while customizing a CSS style: A step-by-step guide

This question is a continuation of my previous inquiries.

I have a CSS file that needs to be included (first_style.css in the code snippet below). The necessary style is img { height:auto; }. Any additional styles can be added before or after this line. Once the images are loaded, a script modifies their height as shown below.

When I exclude first_style.css, everything functions correctly. However, when it is included, which is required, the script stops working. How can this issue be resolved?

$(function() {
  $('.images div a img').on('load', function() {
    $(this).attr('height', $(this).closest('.images').attr('imgheight'));
  });
});
img {
  height:none !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

<link rel='stylesheet' href='http://www.yu51a5.com/wp-content/themes/pinboard-child/first_style.css' type='text/css' media='all' />

<div imgheight="300px" class="images">
  <div><a> <img src=" http://www.yu51a5.com/wp-content/uploads/2015/01/goldenhorseman.jpg " /></a> </div>
  <div><a> <img src=" http://www.yu51a5.com/wp-content/uploads/2015/01/bronze-horseman.jpg "/></a> </div>
</div>

Answer β„–1

Here is a solution to your issue:

$(function() {
  $('.images div a img').on('load', function() {
    $(this).css('height', $(this).closest('.images').attr('imgheight'));
  });
});
img {
  height:none !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

<link rel='stylesheet' href='http://www.yu51a5.com/wp-content/themes/pinboard-child/first_style.css' type='text/css' media='all' />

<div imgheight="300" class="images">
  <div><a> <img src=" http://www.yu51a5.com/wp-content/uploads/2015/01/goldenhorseman.jpg " /></a> </div>
  <div><a> <img src=" http://www.yu51a5.com/wp-content/uploads/2015/01/bronze-horseman.jpg "/></a> </div>
</div>

The resizing will occur after the images finish loading due to the onload event.

Answer β„–2

Below is the functioning fiddle corresponding to your query :

https://jsfiddle.net/utrsrb70/

HTML Markup:

<div imgheight="300px" class="images">
  <div><a> <img class="myimg" src=" http://www.yu51a5.com/wp-content/uploads/2015/01/goldenhorseman.jpg " /></a> </div>
  <div><a> <img class="myimg" src=" http://www.yu51a5.com/wp-content/uploads/2015/01/bronze-horseman.jpg "/></a> </div>
</div>

JavaScript Functionality:

$(function() {

  $('.images img').each(function(){
    console.log($(this).attr('src'));

    $(this).attr('height', $(this).closest('.images').attr('imgheight'));

  })


});

Cascading Style Sheet (CSS):

<link rel='stylesheet' href='http://www.yu51a5.com/wp-content/themes/pinboard-child/first_style.css' type='text/css' media='all' />

Answer β„–3

If you want to achieve this, you can utilize the data-height attribute and access that data attribute in your script by using element.data('height');

Here is a practical example:

$( document ).ready(function() {
$('.images img').each(function() {
      $(this).attr('height',$('.images').data("height"));
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div data-height="300px" class="images">
  <img src="http://www.yu51a5.com/wp-content/uploads/2015/01/goldenhorseman.jpg" />
  <img src="http://www.yu51a5.com/wp-content/uploads/2015/01/bronze-horseman.jpg" />
</div>

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

Exploring Jstree's renaming feature

My jstree script's 'rename node' function isn't working, and I'm not sure why. Here is the code I'm using: $(function () { $("#stworz_powiazanie").click(function () { $("#pages-wrapper").jstree("create"); }); ...

Angular utilizing external parameter for Ajax requests

As a newcomer to Angular, I am eager to upgrade some old jQuery code with AngularJS. The task at hand is to extract a string from a span element, split it into two separate strings, and then use these as parameters in a GET request. I am dedicated to lea ...

What is the process for setting a background image in a React application?

I need help figuring out how to add a background image to my React app. I've searched for solutions on StackOverflow, but I'm still unsure. The image I want to use is named background.jpeg and it's located in the same folder as my App.js fil ...

When the document is ready, set the value in TinyMCE

Using tinymce4.2.6, I have inserted the following code in my master page: <script type="text/javascript"> tinymce.init({ selector: "textarea", directionality: 'rtl', plugins: [ "advlist autolink l ...

two divs side by side with adjustable sizes

Is there a way to achieve a typical forum post layout with user info on the left and the post itself using CSS? I tried wrapping two divs around another div and using float, but encountered an issue where the floated div remains on the left instead of next ...

Guide to using jQuery to load an image

I am using this code to load an image and display it within a dialog box. <div id="image_preview" title="Client Photo Preview"> <p><img src="" alt="client image" id="client_image_preview" /></p> </div> $("#client_image_p ...

JQuery AJAX: Status 0 is displayed in Firefox, Chrome, and Safari browsers, but works fine in Internet Explorer

I currently have two separate applications running: AppA on localhost:1834 AppB on localhost:3597 The html page of AppB is trying to call a method from AppA using the following code snippet: jQuery.support.cors = true; $.ajax({ url: 'http://lo ...

JavaScript function not executing

Within a panel in an UpdatePanel, there is both a dropdown list and a file upload control. The goal is to enable the Upload button when a value is selected from the dropdown and a file is uploaded. This functionality is achieved through a JavaScript functi ...

Is there a way to generate bootstrap divs by parsing csv text?

As I am working on a Bootstrap 3 image gallery, I have noticed that there are multiple recurring divs with a similar structure as shown below: <figure class="col-1 picture-item" data-groups='["groupA"]' data-date-created="2018" data-title=" ...

Adding a class to an element without using an ID or existing class in JavaScript

Just starting out... :) I'm working on this code snippet: <div class="menu"> <ul> <li></li> ... </ul> </div> I want to add the class "nav" to the <ul> element so it looks like ...

Is there a way to eliminate a style from the final element of a list?

I have just completed a project that looks similar to this: I am wondering if there is a way to remove the line at the end of the last column? (it's within a PHP while loop) Here is the code snippet: <div style="border-bottom: 1px solid #cdc ...

Create a basic chart using JavaScript

I am looking to replicate a specific chart using JavaScript. The red point in the chart is variable. So far, I have attempted to create it using the flot library: var d3 = [[7,7]]; $.plot("#placeholder", [{ data: d3, points: { show: true } }], { ...

What is the best way to align the export button group in DataTables?

Recently, I encountered an issue with aligning a DataTables export button group with the search input box. I have utilized Datatable to automatically render the export buttons within a table, but I am struggling to get them to align properly. Below is the ...

Iterate through a JavaScript array and dynamically populate multiple HTML elements with the values using a loop

I need a way to populate an array with numbers and dynamically insert those values into my HTML code in pairs. For instance, I want the value at array index 1 [0] to be 53498 and appear in both the value attribute and within the <div class="n1" ...

Timer Does Not Appear to be Counting Down

I recently added a countdown clock to my website, but I'm having an issue with it not updating in real-time unless the page is refreshed. I'm not very familiar with javascript, so I found some code online and made some modifications myself to sui ...

Removing specific characters from HTML code with ASP

Can someone help me understand how to make the page display HTML code while excluding specific characters like this one (ΓΌ)? ...

Adjusting canvas context position after resizing

I've been experimenting with using a canvas as a texture in three.js. Since three.js requires textures to have dimensions that are powers of two, I initially set the canvas width and height to [512, 512]. However, I want the final canvas to have non-p ...

Navigating absolute URLs in a localhost environment can be managed effectively by following

Ensuring that paths to pages and images are consistently accurate regardless of file hierarchy is important for my PHP website. However, I find it challenging to determine the most efficient and sustainable approach to achieving this. Currently, I am usin ...

Is there a way to deactivate JavaScript upon leaving a div with the mouse?

Is it possible to disable JavaScript when moving the mouse out of a div? I have implemented code using onmousemove to adjust the width of the left div ID. However, I noticed that JavaScript continues to work even when I move the mouse out of the container ...

Struggling to align the h1 tag with a p tag on the same line

How can I align the balance to the upper right-hand corner of my website? I'm new to HTML and CSS and struggling to get the p tag next to the h1 tag. I've tried using display inline for the h1 tag and float right for the p tag, as well as setting ...