Vikki
Vikki

Reputation: 279

How to get value by class name in JavaScript or jQuery?

I wan to retrive all the value of this code using class name. Is it possible in jQuery? I want to retrive only the text within a div or number of div may be change the next form.

  <span class="HOEnZb adL">
    <font color="#888888">
    </br>
    <div>
      <i><font color="#3d85c6" style="background-color:#EEE"></i>
    </div>
    <div>
       **ZERONEBYTE Software** |  
       <a target="_blank" href="http://www.example.com">
       **www.zeronebyte.com**
       </a>
       <a target="_blank" href="mailto:[email protected]">
       **[email protected]**
       </a>
       </br>
    </div>
    <div>
     <div>
      <div>
        **+91-9166769666** | 
        <a target="_blank" href="**mailto:[email protected]**"></a>
      </div>
     </div>
    </div>
   </font>
  </span>

Upvotes: 8

Views: 151228

Answers (3)

RobG
RobG

Reputation: 147513

Without jQuery:

textContent:

var text = document.querySelector('.someClassname').textContent;

Markup:

var text = document.querySelector('.someClassname').innerHTML;

Markup including the matched element:

var text = document.querySelector('.someClassname').outerHTML;

though outerHTML may not be supported by all browsers of interest and document.querySelector requires IE 8 or higher.

Upvotes: 3

Kiran
Kiran

Reputation: 20293

Try this:

$(document).ready(function(){
    var yourArray = [];
    $("span.HOEnZb").find("div").each(function(){
        if(($.trim($(this).text()).length>0)){
         yourArray.push($(this).text());
        }
    });
});

DEMO

Upvotes: 3

Arunkumar Vasudevan
Arunkumar Vasudevan

Reputation: 5330

If you get the the text inside the element use

Text()

$(".element-classname").text();

In your code:

$('.HOEnZb').text();

if you want get all the data including html Tags use:

html()

 $(".element-classname").html();

In your code:

$('.HOEnZb').html();

Hope it helps:)

Upvotes: 27

Related Questions