views:

1445

answers:

5

I have a page containing the following div element:

<div id="myDiv" class="myDivClass" style="">Some Value</div>

How would I retrieve the value ("Some Value") either through JQuery or through standard JS? I tried:

var mb = document.getElementById("myDiv");

But the debugger console shows "mb is null". Just wondering how to retrieve this value.

---- UPDATE ---- When I try the suggestion I get: $ is not a function

This is part of a JQuery event handler where I am trying to read the value when I click a button. The handler function is working but it can't interpret the jQuery value it seems:

jQuery('#gregsButton').click( function() { var mb = $('#myDiv').text(); alert("Value of div is: " + mb.value); } );

+4  A: 
$('#myDiv').text()

Although you'd be better off doing something like:

<div id="myDiv"><p>Some Text</p></div>
<script type="text/javascript">
   $(function(){
       var txt = $('#myDiv p').text();
       alert(txt);
    };
</script>

Make sure you're linking to your jQuery file too :)

Jason
+1 - and what if `myDiv` contains other elements that also contain text nodes? How would the OP get only the text of `myDiv` then?
Russ Cam
+1 for actually answering the question of getting the value using jQuery, not just plain JavaScript!
Sohnee
+1  A: 

if you div looks like this:

<div id="someId">Some Value</div>

you could retrieve it with jquery like this:

$('#someId').text()
Zenon
A: 
myDivObj = document.getElementById("myDiv");
if ( myDivObj ) {
   alert ( myDivObj.innerHTML ); 
}else{
   alert ( "Alien Found" );
}

Above code will show the innerHTML, i.e if you have used html tags inside div then it will show even those too. probably this is not what you expected. So another solution is to use: innerText / textContent property [ thanx to bobince, see his comment ]

function showDivText(){
      divObj = document.getElementById("myDiv");
      if ( divObj ){
       if ( divObj.textContent ){ // FF
        alert ( divObj.textContent );
       }else{ // IE   
        alert ( divObj.innerText );  //alert ( divObj.innerHTML );
       } 
      }  
     }
Rakesh Juyal
Keep getting the message "myDivObj" is NULL in the debugger. I'm wondering if the fact that this div's value is dynamic and populated by javaScript is the cause of the problem. It is a counter is is constantly incrementing.
GregH
It was the "innerHTML" that was needed. Got...works great.
GregH
There's no such property as `text`, there's `textContent` (DOM Level 3 Core standard) and `innerText` (IE-only). `text()` is the jQuery method that calls either of those.
bobince
+1 for textContent
Rakesh Juyal
+1  A: 

You could use

jQuery('#gregsButton').click(function() { 
    var mb = jQuery('#myDiv').text(); 
    alert("Value of div is: " + mb); 
});

Looks like there may be a conflict with using the $. Remember that the variable 'mb' will not be accessible outside of the event handler. Also, the text() function returns a string, no need to get mb.value.

Sandro
Sorry, but I'm still very new to this...the problem with the proposed solution above is that the debugger now gives me an error for the line: var mb = jQuery('#myDiv').text()Saying "JQuery" is not recognized...its like the JQuery library is not accessible from inside the function.
GregH
Should have said the message in the debugger is "JQuery is not defined"
GregH
A: 

You could also use innerhtml to get the value within the tag....

Hulk