How to get value inside a div element using JS

Viewed 221

Here I have a div element with class med and I want to access the value inside the div of the

Prescribed:{{pro_id.prescribedmed}}

.I tried JS to get the value by using the getattribute() but it does not show. how can get the value of it. Here is the piece of code

 <div class="column">
        <!-- style="background-color:#bbb; -->
        <p class="newarrival ">Rs.{{pro_id.price}}</p>
        <h4>{{pro_id.name}}</h4>
        <p><strong>Category:</strong> {{ pro_id.category}}</p>
        <p><strong>Description:</strong> {{pro_id.smalldescription}}.</p>
        <div class="med hidden" >
            <p>Prescribed:{{pro_id.prescribedmed}}</p>
        </div>
        <button data-product={{pro_id.id}} data-action="add" type="button" class="btn-cart update-cart"> Add to cart
       </button>
    </div>

i tired to get the element like

var premed = document.getElementsByClassName('med').innerText;
console.log(premed)

here the output gives like undefined

3 Answers

If I had to do that it would look next: First select the element with Document.querySelector(). Then extract the text of the element. And finally use the String.prototype.substring() method to get the needed value.

You cant get the attribute because you dont have one.

Div element:

<div class="med hidden" id="Div_id" value="Value_here" >

JS:

var value = document.getElementById("Div_id").getAttribute ("value")

Give this a try

<p class="med_prescribed">Prescribed: {{ pro_id.prescribedmed }}</p>

in your JS as follows

var premed = document.getElementsByClassName('med_prescribed')[0].innerText;
console.log(premed);
Related