Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 352
Vikki Avatar asked Feb 25 '14 04:02

Vikki


People also ask

How do you target a class name in JavaScript?

To select a specific HTML class using JavaScript, we need to target it and then store it as a variable. Here is the one line of JavaScript we need to target this element and store it as a variable: Code from a text editor: const vanillaDescription = document. querySelector('.

How do you target a class in jQuery?

In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.

How do you check if an element has a specific class in jQuery?

jQuery hasClass() Method The hasClass() method checks if any of the selected elements have a specified class name. If ANY of the selected elements has the specified class name, this method will return "true".

How do you select element by id in jQuery?

The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.


2 Answers

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:)

like image 99
Arunkumar Avatar answered Sep 28 '22 10:09

Arunkumar


Try this:

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

DEMO

like image 45
Kiran Avatar answered Sep 28 '22 09:09

Kiran