I need to use JavaScript to get the username which is posted in the following way:
<div id="project_user">
<p class="project_user">
by <span class="project_username"><?php echo $OwnerName; ?></span>
</p>
</div>
How do I do it?
I tried the following but it didn’t work:
document.getElementById('project_username').innerHTML
You select by
idwhile you need to select by theclassName:Live DEMO
Note that
document.getElementsByClassNameis not implemented in old IE versions…Another way:
Live DEMO
You can use this “patch” to define this function in old browsers:
source
Or use a javascript library like
jQuerythat implements (almost) all css selectors, example: