Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value of div content using jquery

Tags:

jquery

I have the following html and I want to get the value of the div which is "Other" How can I do this with jQuery?

 <div class="readonly_label" id="field-function_purpose">
        Other
 </div>
like image 330
redcoder Avatar asked Oct 25 '13 05:10

redcoder


People also ask

How do I get HTML inside a div using jQuery?

First, use the html() function instead of the text() function. Second, point your selector to the right node, something like . portlet-content . content_regular table .

Can a div have a value attribute?

An <div> element can have any number of data-* attributes, each with their own name.

How get data attribute value in jQuery?

Answer: Use the jQuery attr() Method You can simply use the jQuery attr() method to find the data-id attribute of an HTML element.

How do I get inner text in jQuery?

Answer: Use the jQuery text() method You can simply use the jQuery text() method to get all the text content inside an element. The text() method also return the text content of child elements.


Video Answer


4 Answers

Use .text() to extract the content of the div

var text = $('#field-function_purpose').text()
like image 116
Arun P Johny Avatar answered Oct 12 '22 00:10

Arun P Johny


your div looks like this:

<div class="readonly_label" id="field-function_purpose">Other</div>

With jquery you can easily get inner content:

Use .html() : HTML contents of the first element in the set of matched elements or set the HTML contents of every matched element.

var text = $('#field-function_purpose').html(); 

Read more about jquery .html()

or

Use .text() : Get the combined text contents of each element in the set of matched elements, including their descendants, or set the text contents of the matched elements.

var text = $('#field-function_purpose').text();

Read more about jquery .text()

like image 35
Ishan Jain Avatar answered Oct 12 '22 01:10

Ishan Jain


You can get div content using .text() in jquery

var divContent = $('#field-function_purpose').text();
console.log(divContent);

Fiddle

like image 24
Roopendra Avatar answered Oct 12 '22 00:10

Roopendra


You can simply use the method text() of jQuery to get all the content of the text contained in the element. The text() method also returns the textual content of the child elements.

HTML Code:

<div id="box">
  <p>Lorem ipsum elit sit ut, consectetur adipiscing dolor.</p> 
</div>

JQuery Code:

  $(document).ready(function(){
    $("button").click(function(){
      var divContent = $('#box').text();
      alert(divContent);
    });
  });

You can see an example here: How to get the text content of an element with jQuery

like image 25
thomas Avatar answered Oct 12 '22 00:10

thomas