Get div content from a child div onclick
I have a wrapper div1, with div2 inside which contains a string. When i click on div开发者_开发知识库1 i want the content from div2 to appear in alert(); how can this be done ?
document.getElementById(feats).addEventListener("click",function show() {
var htmlstring = this.innerHTML;
alert(htmlstring);
The code above simply just add an even listener and alert all the html code from the wrapper.
Thanks
With jQuery, which is well worth learning:
$('#div1').click(function() {
alert($('#div2').text());
});
This does what you want:
<html>
<head></head>
<body>
<div id="div1" onclick="doAlert();" style="cursor: pointer; cursor: hand;">
<div id="div2">
Hello, this is the contents of div2
</div>
</div>
<script type="text/javascript">
function doAlert() {
alert(document.getElementById("div2").innerHTML);
}
</script>
</body>
</html>
Also, gentle nudge, you will get more responses if you start accepting answers.
精彩评论