Inquire about the width of the SVG text element

I am working with an SVG text element that looks like this:

<text data-model-id=​"k10673" x=​"584" y=​"266" fill-opacity=​"1" style=​"font:​ 12px Arial,Helvetica,sans-serif;​ " fill=​"#a5d16f">​BU YIL SONU​</text>​

The text is positioned approximately in the middle of the screen. When I inspect it using Chrome debugger, I can see that its width is 83.941.

However, when trying to retrieve this width from my JavaScript page, all methods related to the width return either zero or undefined, even though Chrome debugger displays it correctly.

This is how I obtain the element:

var x = document.querySelector("#k11489 > g:nth-child(27) > text:nth-child(1)")

Subsequently, the following JavaScript functions provide no width value:

x.getComputedTextLength()
x.getBBox().width
x.style.width

Similarly, jQuery functions I tried also do not fetch the width:

x.width() //Returns the function
x.innerWidth() //Returns null
x.outerWidth() //Returns null

Please note: To ensure that 'x' points to the correct text element, I verify it through the console. Here are snapshots of the JavaScript and jQuery objects:

Is there a way to accurately retrieve the width of this object using JavaScript or jQuery?

Answer №1

Not sure if this method applies to text elements, but in my experience, using the

document.querySelector('.whatever').getBoundingClientRect().width;

does work and takes into account the current scaling of the SVG. You can also check out this solution - by modifying the provided code snippet to target a text node, I was able to achieve success.

Answer №2

To retrieve the width of an SVG element, you can utilize the getBBox() method available in SVG 1.1 like this:

$("your_jquery_selector")[0].getBBox().width.

If the selector is valid, you may also use:

$("#k11489 > g:nth-child(27) > text:nth-child(1)")[0].getBBox().width

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

React - Updating the document title upon user navigation away from the current page

I have encountered a challenge and I am seeking guidance on how to resolve it. Throughout our website, all pages have a default title based on our index.html file: <html lang="en"> <head> <title>My Page Title</title> ...

Conditional statement based on the background color

Despite researching various topics on using background colors in if statements, I have yet to find a satisfactory solution. Whether I define an element as a variable beforehand or use rgb/rgba values, my code does not produce the desired results; it always ...

I am looking to halt the AJAX requests within an asynchronous function after reaching a limit of 10 requests

I've been working on an async function that sends AJAX requests based on the previous response. The function is functioning properly, but it's sending multiple requests in quick succession. I need to implement a 5-second interval between each req ...

populating all available space in layouts using bootstrap columns

I am attempting to create a grid layout using bootstrap, but I am facing an issue with positioning one of the columns. I have tried adjusting the placement of the divs and using offsets/pulls, but so far I have not been successful. To explain what I am a ...

What is the reason for the absence of the $.ajax function in the jQuery package designed for node.js?

Here is my code sample, which I would like to use for practicing with jQuery's ajax function. Note that I have already installed the jQuery package using npm install jquery: var $ = require('jquery'); var remoteValue = false; var doSometh ...

Why is Python BeautifulSoup's findAll function not returning all the elements in the web page

I am attempting to retrieve information from the following URL . Displayed below is the code I have developed. import requests from bs4 import BeautifulSoup url_str = 'https://99airdrops.com/page/1/' page = requests.get(url_str, headers={&apo ...

Tips for causing a column to move to the following line once it exceeds the width of the div

My menu is exceeding the boundaries of its container. How can I make sure the overflowing content moves to a new line while staying within the parent element? Check out an example here Here is the structure of my HTML: <div class="product-menu" style ...

Scrolling vertically within the Windows 8 FlipView

Can a WinJS FlipView support vertical scrolling? It appears that the FlipView may be preventing the scroll event from functioning properly. Appreciate any help in advance ...

Issue with JSONP success function not being triggered

Here is an example of an Ajax call being made to a different domain: <script> $.ajax({ url: 'url?callback=?', dataType: 'jsonp', success: function (data) { alert(data[0].DeviceName) ...

The TypeScript script does not qualify as a module

Just starting out with TypeScript and encountering a simple issue. I'm attempting to import a file in order to bring in an interface. Here's an example: Parent: import { User } from "@/Users"; export interface Gift { id: number; ...

Exploring the Power of Arrays, For-Loops, and Functions in JavaScript

Currently, I have a functional code implementation, but I'm struggling with creating a proper loop. I'm seeking a solution to develop a function that changes the URL source of #mainImage to the one in the imgUrl array when hovering over an #id i ...

Utilizing React to highlight buttons that share the same index value upon hover

I have some data in a JavaScript object from a JSON file, where certain entries have a spanid (number) while others do not. I've written React code to highlight buttons with a spanid on hover, but I'm looking for a way to highlight or change the ...

Conceal a div based on user role on a single webpage

I'm looking for a way to prevent a specific user role from seeing a specific div on one of the pages of my website. I attempted to add the code below to my theme's function.php file, but unfortunately it did not have the desired effect. Do you ha ...

Creating a Vue.js component during the rendering process of a Laravel Blade partial view

In my Vue.js project, I have a component that is used in a partial view called question.blade.php: {{--HTML code--}} <my-component type='question'> <div class="question">[Very long text content...]</div> </my-component& ...

Excess characters are appearing beyond the end of the <html> tag

I've been troubleshooting a website and recently noticed that some extra characters are appearing at the end of the pages. Specifically, it seems that the "greater than" character ( > ) is being inserted automatically when accessing a page through ...

Tips for monitoring input content "live"

Currently, I am in the process of developing a web form that includes a text field intended to receive numeric values. If the user enters non-numeric characters into this field, the form will not submit. However, there is no error message displayed to noti ...

How to efficiently handle conditional rendering in React when passing data from Children to Parent

Just starting out with React, currently exploring react 16.13 and looking into how to achieve the following: Sending data from Child Components to Parent Components If the passed data is "true", display the specific Child component on the Parent page. We ...

The image selection triggers the appearance of an icon

In my current project, I am working on implementing an icon that appears when selecting an image. The icon is currently positioned next to the beige image, but I am facing difficulties in making it disappear when no image is selected. Below are some image ...

Utilize the ID attribute for naming arrays in jQuery

We have multiple links enclosed in a block: <div> <a href="#" id="john">text</a> <a href="#" id="mary">text</a> <a href="#" id="emma">text</a> </div> Each link has a unique id. I am attempting ...

Ways to determine when a page has reached the end of its scrollable content

I'm working on a web page that extends beyond the viewport, requiring users to scroll or swipe to see the entire content. I want to dynamically change the message from "swipe right" to "swipe left" based on the user's scrolling behavior. However, ...