Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript get inner HTML text for span by class name

This is the basic format of the code the table is contained within a div named

<div class="leftCol">    
.....
<tr id="my_cd">
<td><span class="agt_span">My Code</span></td>
</tr>
.....
</div>

I need to be able to get whatever text is contained within the span class, in this case I need to pull the text "My Code" and then add that into an array. Adding the text into an array is not the issue that's easy but I can't figure out how to pull the text. No matter what I try I can't get anything but an 'undefined' value.

How do I get the Inner HTML text value for a span by class name?

First Question solved thanks!!

Second question expand on first:

<div class="leftCol">    
.....
<tr id="my_cd">
  <td><span class="agt_span">My Code</span></td>
  <td>
    <div>
      <select name="agt_drp" id="agt_drp" class="agt_drp">...</select>
    </div>
  </td>
</tr>
</div>

Let's say I have the select id "agt_drp" and I want to get the span class text. Is there any way to do that?

like image 396
George Gervin Avatar asked Jun 23 '15 15:06

George Gervin


People also ask

How do I get text inside a span?

Use the textContent property to get the text of a span element, e.g. const text = span. textContent . The textContent property will return the text content of the span and its descendants. If the element is empty, an empty string is returned.

How do I find the inner span in HTML?

getElementsByTagName("span"); var txt; for(i in spans) { if(spans[i]. getAttribute("class"). contains("agt_span")){ txt = spans[i].

Can we use innerHTML in span?

Bookmark this question.

Can HTML span have a class?

Classes (i.e. classnames) are used for styling the span element. Multiple classnames are separated by a space. JavaScript uses classes to access elements by classname.


1 Answers

Jquery:

var test = $("span.agt_span").text();
alert(test):

Javascript: http://www.w3schools.com/jsref/met_document_getelementsbyclassname.asp

like image 192
NubPro Avatar answered Sep 22 '22 15:09

NubPro