javascript get html or text from div
This is easy in jquery but how about plain javascript?
The code is for the Google image search API. I want to search images that match the html or preferably text inside a div.
var searchThis = document.getElementById('imgToFind'); //get HTML, or text from div
// Find images
imageSearch.execute(searchThis); //search image
The HTML
<div id="imgToFind">PlayStation</div>
The result should be images of playstation. Instead开发者_Go百科 I get these
dosic ois - 123 IE8 javascript returns [object HTMLDivElement] instead [ Loving HandAnd other irrelevant images. I wonder what Google searches for.. think the second one is giving me a hint.
its var searchThis = document.getElementById('imgToFind').innerHTML;
to extract only text you can do:
element = document.getElementById('imgToFind');
var searchThis = element.textContent || element.innerText;
var searchThis = document.getElementById('imgToFind').innerHTML;
will grab the contents of the element :)
If you want just the text within an element, you can use textContent
in the more standards compliant browsers (i.e. not IE) and innerText
in IE. They're not identical in all cases but for your purposes they may well be.
var el = document.getElementById("foo");
var text = el.textContent || el.innerText;
精彩评论