html – How to get value from element JavaScript-ThrowExceptions

Exception or error:

How can I get the value from this element:

<div time="1582408800000" title="" class="day toMonth  valid real-today checked first-date-selected">23</div>

I have tried the following:

//web.getAttribute('//div[@class="day toMonth  valid real-today"]', div)

var bsaleDate = web.getValue('//div[@class="day toMonth  valid real-today"]');

I’m trying to get the 23 as a value and insert it into a variable.
I am using Oxygen IDE which is based on JavaScript.

How to solve:

You could use DOM selector to select the element and then get the content.

Note that div element doesn’t has the value attribute, Div element could hold html or text content.

To get the html content we could use innerHTML attribute.
And to get text we could use textContent attribute

I am using document.querySelector to select the element with the help of css selector.

(function() {
    let ele = document.querySelector('.first-date-selected');
    console.log(ele.textContent);
})()
<div time="1582408800000" title="" class="day toMonth  valid real-today checked first-date-selected">23</div>

###

You need to use the getElementsByClassName method to get all the elements of specific class.

var x = document.getElementsByClassName(“day”);

IF you want to Get all elements with “day toMonth valid real-today checked first-date-selected” classes, you can just use below statement.

var x = document.getElementsByClassName(“day toMonth valid real-today checked first-date-selected”);

alert(x[0].innerText);

Hope this could solve your problem.

###

by value if you meant ’23’ is what you wanted. Then you can use getText().

Leave a Reply

Your email address will not be published. Required fields are marked *