17

How to get inner text of a DIV controller using java script?

Ajeesh Ajayan
  • 315
  • 1
  • 2
  • 5
  • Check out this previously answered question on stackoverflow --> http://stackoverflow.com/questions/5284118/how-to-use-simple-html-dom-get-a-div-inner-text – skywalker2909 Apr 09 '13 at 14:08

2 Answers2

38

The short answer:

document.getElementById("id-of-div").innerText

The long answer, given that you've tagged the question with asp.net-mvc-3, is that this will be run in the browser, not on the server (where ASP.NET runs). There is no immediate way to get content from the browser to the server without sending a request. I'm guessing that you may want to make an ajax call to a new controller action from the page, but this depends on when the text changes, and what you want to do with it.

harriyott
  • 10,505
  • 10
  • 64
  • 103
  • The **innerText** property is valid for **block elements only**. By definition, elements that do not have both an opening and closing tag cannot have an innerText property. [link](http://msdn.microsoft.com/en-ca/library/ie/ms533899%28v=vs.85%29.aspx) – dhruvpatel Jun 30 '14 at 17:17
  • 4
    A div is a block element though. – harriyott Nov 26 '14 at 14:11
8

Suppose you have a div declared as:

<div id="someDiv">
  some content goes here
</div>

You can get its value by:

// javascript
document.getElementById("someDiv").innerHTML
// jquery
$("#someDiv").html()
von v.
  • 16,868
  • 4
  • 60
  • 84