Hi there!
I’m trying to get information from a class with javascript executing the parameter document.getElementsByClassName(“className”); without any lucky.
This is the real case:
<div id="uploadcare--widget__text" class="uploadcare--widget__text" aria-live="polite">
**<div class="uploadcare--link uploadcare--widget__file-name" tabindex="0" role="link">1 (1).mp4</div>**
I added the following code with the toolbox pluging - javascripttoBubble in page:
bubble_fn_1(document.getElementsByClassName("uploadcare--link uploadcare--widget__file-name").value)
Calling a Div Id I didn’t have any issues.
Note1: I found this solution from the post of @mebeingken ([SOLVED] Get data from HTML Element). Thank you!
Note2: I didn’t find any post with this trouble.
Any ideas?
Cheers.
getElementsByClassName
returns an array whereas getElementById
returns a single value.
Try adding [0]
to the return value to access the first item in that array: document.getElementsByClassName("uploadcare--link uploadcare--widget__file-name")[0]
2 Likes
Thank you for your response! I tried but I still without getting the text.
I’m not sure what .value
does in your example?
As far as I know it’s not something that’s included as part of the return from getElementsByClassName()
method. Where does that come from?
Check out this simple example here of what I think you’re trying to do.
2 Likes
@exception-rambler I don’t know the reason of “.value”, I took it from the original post of this issue.
I test different alternatives from the browser console and I found the solution execution this:
document.querySelector('.uploadcare--widget__file-name').textContent;
By the way, I got the response from ChatGPT.
Thank you @exception-rambler.
ChatGPT is scary good…
I’m just adding my 2 cents here: textContent
gets the content of all elements, including <script>
and <style>
elements. In contrast, innerText
only shows “human-readable” elements. In general it is better to use innerText
.
1 Like