How to use jQuery to set the padding-right equal to the width of a child element

My goal is to set the padding-right css property based on the width of a child element. Here's my approach:

Using jQuery

$('.parent_element').css('padding-right', $(this).children('.child_element').width() + 'px');

Here's the HTML structure:

<a href="#" class="parent_element"><i class="fa"></i> Example <span class="child_element">101</span></a>

However, this code doesn't apply the desired css to the parent element.

Answer №1

If you want to dynamically set the padding-right of an element based on its child elements, you can achieve this by using a function as the second argument in the jQuery .css() method.

$('.parent_element').css('padding-right', function () {
  return $(this).children('.child_element').width() + 'px';
});

You can see an example of this implementation here.

Answer №2

this in this snippet of code will represent the value of this within the current scope, rather than the intended target of the .css method. In order to achieve the desired outcome, a new scope must be created. This can be accomplished by passing a function to the .css method that returns the desired value:

$('.parent_element').css('padding-right', function(){
    return $(this).children('.child_element').width() + 'px'
});

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

Is it impossible to use CSS for absolute positioning of text?

I am currently learning CSS and HTML, as I'm working on a project for my final school assignment. My goal is to position the text "Welcome" absolutely within a specific div that I've created. However, despite multiple attempts, I can't seem ...

Display and unveil Bootstrap modals

In my Angular5 web application, I am looking to switch from one modal to another while utilizing bootstrap components and Jquery. $('#myModal').modal('show'); $('#myModal1').modal('hide'); However, I have encounter ...

Enhance Your Website with Dynamic Autocomplete Using AJAX

I am attempting to populate the bootstrap autocomplete(Typeahead) list with data from an external Web-service called "Wunderground Weather", but it is not functioning correctly. An error message mentioning "hasOwnProperty" is being displayed. <link re ...

I'm having trouble getting the animation to work with jQuery. Despite trying everything, the animation doesn't seem to respond to any button clicks

Issue: I am facing a problem where nothing happens when I click the menu on the HTML page. It seems like a total beginner mistake, but I can't seem to get it to work. I have checked the code, and alert works fine, indicating that jQuery is connected t ...

What is the best way to ensure that all my jQuery elements function seamlessly in Joomla?

I am currently working on a Joomla 3.2 template that includes a touch-activated menu instead of the traditional rollover menu. Although Joomla automatically loads jQuery, I have encountered an issue with my menu script not functioning properly alongside th ...

Printing tables with multiple rows in separate pages using jQuery

To achieve the desired result of dividing multiple table rows into separate pages when printing, a script has been created. Each page can contain up to 21 rows from the tables. Any assistance in implementing this functionality would be greatly appreciated ...

Challenges with Implementing Background Images on my Website with HTML and CSS

Being new to the world of coding, I recently created a website for a college presentation. However, after uploading the website onto the internet, I noticed that the background images from my files are not displaying on the website. It's likely that I ...

Display the Bootstrap datepicker within an h4 element set to default to today's date, utilizing both Angular and jQuery

Utilizing Boostrap datepicker to obtain the selected date from the calendar and insert it into an <h4> html tag. However, my goal is to display today's date by default in the h4 when it opens for the first time. Using angular.js + jquery f ...

Ajax loader keeps spinning even when not triggered by function (following an AJAX request)

Currently, I have incorporated a spinner into my AJAX calls. The binding code has been placed within a function to allow flexibility in its usage and prevent specific AJAX calls from triggering the spinner display. For example, I have implemented username ...

Tips for accessing the HTML code of a TextBox in JavaScript while utilizing HTMLEditorExtender

Currently, I am utilizing the HTMLEditorExtender ajax tool on my website. The data is being saved in HTML format into the database and then retrieved within my project without any issues. However, there is a setback that I have encountered... When attemp ...

What are the techniques used to minimize JavaScript functions?

Is there a more efficient way to reduce the amount of JavaScript code needed for functions that share the same call procedure? I'm struggling to find a clear explanation but here's the code snippet below... JavaScript: $(function() { $( &a ...

Adjusting canvas dimensions for angular chart.js: A quick guide

I am currently creating my first sample code using angular chart.js, but I am facing an issue with changing the custom height and width of my canvas. How can I adjust the height and width accordingly? CODE: CSS #myChart{ width:500px; he ...

Tips for sending a JSON array to a Java servlet using jQuery

I'm currently working on a spring mvc project and I am trying to send a JSON array. When I use the method request.getParameter("paramValue") to retrieve the parameter value, it is returning as null. Below is the code snippet from my front-end: $.aj ...

CSS is altering the background color to a slightly modified tint from its original shade

After uploading a high-definition image as a background, I noticed that in the DOM the file appeared to have a slightly greener tint than the original. However, upon inspecting the image background element and saving it again, it saved with the original ti ...

Utilizing jQuery and CSS to make an entire div clickable, and activate an 'a' tag hover state upon hovering

Looking to create a clickable link for the .wrapper div that directs users to the location of a.icon. Want the .wrapper div to activate the a.icon:hover state when hovered over, not just when hovering over the icon itself. Any assistance would be highly a ...

What is causing the HTML sub menu to appear in the incorrect position?

I'm working on an HTML and CSS menu. I'm trying to center the sub-menu relative to the main menu (ul.menu). Why is there extra space appearing on the left side? The styling for ul.menu li:hover ul already sets left: 0, and its closest parent i ...

Using jQuery to append a string to a CSS property

My current task involves modifying a series of divs with background images named Look-1.jpg to Look-6.jpg. I am looking to add "-cut" to all of them simultaneously to transform them into Look-6-cut.jpg. I have two collections of background images and I ai ...

Tips for arranging multiple divs in a parent div in a horizontal alignment with uniform space in between

Although there are numerous inquiries concerning this topic, I am having trouble sorting it out. As a newcomer to CSS, I am attempting to replicate the straightforward box within a box technique. You can view my Codepen here. Additionally, here is the HT ...

Learn the process of dynamically updating the source of an HTML5 video

Currently, I am working on a project that involves dynamically loading multiple videos onto a webpage. The structure of my HTML is quite straightforward - it consists of a single video tag. <video controls preload width="520" height="350" id="video"> ...

What could be causing the PHP JSON response to fail in sending the data array back to the AJAX jQuery request?

After making an ajax request to the PHP server, I am sending data from a form request and expecting to receive the same data back from the server for testing purposes. This will help me analyze the requests and responses between the client and the server. ...