Ways to dynamically retrieve the width of a div's content

I'm currently working on an AngularJS application and I have a div that's structured like this:

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.10/angular.min.js"></script>
<div class="role">
  Sign Up
</div>

Is there a way for me to dynamically determine the width of the content inside the div based on the screen size? In the image provided, the width of Sign Up is noted as 52.36px.

https://i.sstatic.net/TZAxa.png

Any assistance would be greatly appreciated. Thank you!

Answer №1

According to @Roko C. Buljn, tackling this task is quite simple:

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.10/angular.min.js"></script>
<div id="foobar" class="role">
  Sign Up
</div>
<script>
  console.log(document.getElementById("foobar").getBoundingClientRect().width)
  console.log(document.getElementById("foobar").clientWidth)
  console.log(document.getElementById("foobar").offsetWidth)
</script>

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

Tips for maintaining aspect ratio when embedding Vimeo videos in HTML?

My goal is to maintain a 16:9 aspect ratio for my videos, but some of them are not 1920x1080. Specifically, two of them have resolutions of 1920 x 1012 and 1000 x 416. When I attempt to embed these videos in an HTML file, they end up distorted with eithe ...

403 Forbidden: You do not have permission to access this resource

Whenever I attempt to execute a npm install on a project that has Angular as a dependency, I encounter the following error: error 403 Forbidden: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84e5eae3f1e8e5f6c4b5aab2aab0"> ...

How to Perfectly Align a Gif

I'm trying to understand the functionality behind this code snippet. Can anyone shed some light on it? img{ display:block; margin:auto; } I attempted to center a gif using this code block after my teacher suggested trying align:center; without succe ...

In terms of performance, is it better to fetch JSON data and render it using JavaScript, or to request the full

Similar Inquiry: Why is it a bad practice to return generated HTML instead of JSON? Or is it? When making an AJAX request to a PHP file, which method would result in quicker HTML rendering? Directly sending fully formatted HTML from PHP, Or sending ...

JS | Attach a variable to the entire class scope

Is it possible to bind a variable (this) to an entire class without needing to pass arguments from one function to another? I have created a code sample on CodePen: https://codepen.io/anon/pen/vRBVRj class Person { constructor(name) { this. ...

Error: Trying to dispatch from an undefined property in Angular and Redux

In the process of developing a Shopping app using Angular + Redux, I encountered an issue. When attempting to trigger the "ADD_PRODUCT" action on click through a dispatcher function, I keep running into the error message: ERROR TypeError: Cannot read prop ...

Can someone explain why the color of a <select> element in Chrome is being influenced by the CSS property "font-family: inherit"? Here is an example

I've always been curious as to why the <select> element appears in different colors on various web pages. I've noticed that when the font-family:inherit css property affects the select element, it ends up looking blue! <!-- BLACK --> ...

What could be causing the 'post' method to malfunction in my Django application?

I encountered an issue while using Django to develop a blog. The post request for obtaining contact information is not functioning as expected. In the contacts view, I am simply trying to confirm that the post request works, but instead of returning a post ...

Unable to scroll horizontally in tr element due to overflow-x issue

I am having an issue with a table that contains a "tr." Within this row, there are several "td" elements and I am trying to enable horizontal scrolling using the "overflow-x: scroll" property but it is not working as expected. <tr id="TR_TESTS_BY_CAT ...

Guidelines for choosing input using jQuery

I am looking to retrieve the value of an input using jQuery. Specifically, I need to extract the value of a hidden input with name="picture" when the user clicks on the حذف این بخش button by triggering the function deleteDefaultSection( ...

What is the best way to evenly distribute the elements of my navigation bar using percentage values?

Hello, this is my first attempt at creating a website and I am currently working on designing a navigation bar with evenly spaced list items and dropdown boxes. While I have successfully implemented the dropdown boxes, I am struggling to evenly distribute ...

What is causing Node to mistake my MongoDB model for a module?

I've set up a basic MERN project with a UserModel included. import mongoose from "mongoose" const UserSchema = new mongoose.Schema({ name: String, email: String, password: String }) const UserModel = mongoose.model('Users', ...

Is there no sign of rails being utilized in production?

There are some classic image png files located in app/assets/images/... However, in production, after running rake assets:precompile The images do not appear! Upon inspecting the CSS source code, I noticed that .logo { background-image: url("/ass ...

Feature in ES6: map_iteration

I have been experimenting with the es6 map data structure, but I encountered an error when trying to iterate through the map. The specific error message is as follows: The error occurs on line 6: for (let [key, val] of m.entries()) SyntaxError: Unexpecte ...

Disabling the authentication prompt in the browser

Apologies for the repetition, but I would like to pose a more general question. Is there any method on the client side of a web application to predict if requesting a resource will result in a 401 status code and trigger an unattractive authentication pro ...

Display the quantity of stock in WooCommerce as separate numerical values

I am looking to display the stock quantity of products on a specific page of my website. The code I am currently using to retrieve the stock quantity is as follows: <p class="stock-m13"><?php echo $product->get_stock_quantity(); ?></p> ...

Is it possible to ensure a div occupies all available space within a column using the vh-100 class in bootstrap?

In the <div class="bg-primary"></div> element, I'm trying to make it take up the remaining empty space without exceeding the vh-100. I've experimented with various solutions but haven't been able to find a fix yet. b ...

Capturing Shadows in three.js: Unveiling the Magic of Shadows on Transparent Materials

Is there a way to create a shadow on a boxMesh without making the mesh itself visible? I came across a technique mentioned on the three.js GitHub Issue Tracker that used to work but no longer does - it required the usage of a custom shader. Are there any ...

Determine the X and Y coordinates of a circle element using HTML and Javascript

I am trying to retrieve the X and Y coordinates of an HTML circle element using JavaScript. Here is the snippet of my HTML code: <svg id="hotspot_canvas" name="hotspot_canvas" class="hotspot" width="800" height="1570.66666667" style="background-ima ...

Angular app with regex patterns varying based on locale (decimal represented by dot or comma)

In my Angular 2 + PrimeNG application, I have a regular expression that validates numbers with floating point: <input pInputText pattern="[0-9]*\.[0-9]" [(ngModel)]="someModel"/> While this works fine for the US locale, some countries use a co ...