Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #39204 > unrolled thread
| Started by | Tatsujin <tatsujin.3.r@gmail.com> |
|---|---|
| First post | 2020-11-26 23:42 -0800 |
| Last post | 2020-11-27 13:55 +0100 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.javascript
Accessing DIV value on current document Tatsujin <tatsujin.3.r@gmail.com> - 2020-11-26 23:42 -0800
Re: Accessing DIV value on current document "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-11-27 13:49 +0100
Re: Accessing DIV value on current document Jake Jarvis <pig_in_shoes@yahoo.com> - 2020-11-27 14:02 +0100
Re: Accessing DIV value on current document Tatsujin <tatsujin.3.r@gmail.com> - 2020-11-27 11:19 -0800
Re: Accessing DIV value on current document Jake Jarvis <pig_in_shoes@yahoo.com> - 2020-11-27 13:55 +0100
| From | Tatsujin <tatsujin.3.r@gmail.com> |
|---|---|
| Date | 2020-11-26 23:42 -0800 |
| Subject | Accessing DIV value on current document |
| Message-ID | <0af79463-50bf-47e3-9e64-16639ec109edn@googlegroups.com> |
If I have the following HTML code, <div class="outerbox"> <div class="c1">Some words here</div> <div class="c2">More words here</div> </div> How do I access (or get a reference to) the text value in DIV class "C2"? I thought maybe it was "document.c2.value", but I was wrong.
[toc] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2020-11-27 13:49 +0100 |
| Message-ID | <XnsAC828C9F45D95eejj99@194.109.6.166> |
| In reply to | #39204 |
ram@zedat.fu-berlin.de (Stefan Ram) wrote on 27 Nov 2020 in
comp.lang.javascript:
> Tatsujin <tatsujin.3.r@gmail.com> writes:
>><div class="outerbox">
>> <div class="c1">Some words here</div>
>> <div class="c2">More words here</div>
>></div>
>>How do I access (or get a reference to) the text value in DIV class "C2"?
>>I thought maybe it was "document.c2.value", but I was wrong.
>
> I'd suggest to add an id to it and then use »getElementById«.
>
> Otherwise, something like
>
> document.getElementsByClassName( "c2" )[ 0 ]
>
> or
>
> document.querySelector( "div.outerbox > div.c2" )
>
> might work, depending on what else is contained in that document.
const C2 = document.querySelectorAll('.C2')[0]; // or other number
or
const C2 = document.querySelectorAll('div')[2]; // or other number
or
const outerbox = document.querySelector('outerbox');
const C1 = outerbox.querySelector('.C1');
const C2 = outerbox.querySelector('.C2');
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Jake Jarvis <pig_in_shoes@yahoo.com> |
|---|---|
| Date | 2020-11-27 14:02 +0100 |
| Message-ID | <rpqtd8$bl0$1@dont-email.me> |
| In reply to | #39206 |
Am 27.11.2020 um 13:49 schrieb Evertjan.:
> ram@zedat.fu-berlin.de (Stefan Ram) wrote on 27 Nov 2020 in
> comp.lang.javascript:
>
>> Tatsujin <tatsujin.3.r@gmail.com> writes:
>>> <div class="outerbox">
>>> <div class="c1">Some words here</div>
>>> <div class="c2">More words here</div>
>>> </div>
>>> How do I access (or get a reference to) the text value in DIV class "C2"?
>>> I thought maybe it was "document.c2.value", but I was wrong.
>>
>> I'd suggest to add an id to it and then use »getElementById«.
>>
>> Otherwise, something like
>>
>> document.getElementsByClassName( "c2" )[ 0 ]
>>
>> or
>>
>> document.querySelector( "div.outerbox > div.c2" )
>>
>> might work, depending on what else is contained in that document.
>
> const C2 = document.querySelectorAll('.C2')[0]; // or other number
>
> or
>
> const C2 = document.querySelectorAll('div')[2]; // or other number
>
> or
>
> const outerbox = document.querySelector('outerbox');
> const C1 = outerbox.querySelector('.C1');
> const C2 = outerbox.querySelector('.C2');
>
>
that last one looks wrong, doesn't it?
there is no OUTERBOX element
there are no classes C1 and C2
[toc] | [prev] | [next] | [standalone]
| From | Tatsujin <tatsujin.3.r@gmail.com> |
|---|---|
| Date | 2020-11-27 11:19 -0800 |
| Message-ID | <329c87f5-3ad4-44d2-9940-c71783febaa7n@googlegroups.com> |
| In reply to | #39208 |
This seems to work: "document.querySelectorAll('div')[2].textContent"
Thanks guys!
[toc] | [prev] | [next] | [standalone]
| From | Jake Jarvis <pig_in_shoes@yahoo.com> |
|---|---|
| Date | 2020-11-27 13:55 +0100 |
| Message-ID | <rpqt09$3l0$1@dont-email.me> |
| In reply to | #39204 |
Am 27.11.2020 um 13:37 schrieb Stefan Ram: > Tatsujin <tatsujin.3.r@gmail.com> writes: >> <div class="outerbox"> >> <div class="c1">Some words here</div> >> <div class="c2">More words here</div> >> </div> >> How do I access (or get a reference to) the text value in DIV class "C2"? >> I thought maybe it was "document.c2.value", but I was wrong. > > I'd suggest to add an id to it and then use »getElementById«. > > Otherwise, something like > > document.getElementsByClassName( "c2" )[ 0 ] > > or > > document.querySelector( "div.outerbox > div.c2" ) > > might work, depending on what else is contained in that document. > > Tatsujin: now that you got the element, read its textContent property https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Node3-textContent
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web