Re: GetElementByClass?
| From: | Nathan Nobbe | Date: | Sat, 03 Apr 2010 16:29:34 +0000 |
| Subject: | Re: GetElementByClass? | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-303792@lists.php.net to get a copy of this message | ||
On Sat, Apr 3, 2010 at 10:18 AM, tedd <tedd@sperling.com> wrote:
> At 8:14 AM -0600 4/3/10, Peter Pei wrote:
>
>> No javascript's getElementByID() won't work here. As "question" is a
>> class, not an ID. But like what was mentioned here, you can use
>> getElementByClass() with Opera, and that will work.
>>
>
> Sort of.
>
> Like I said, the folling will work:
>
> document.getElementById("question").innerHTML;
>
> While you are using a getElementById, which returns an ID, but adding
> .innerHTML will return the class value.
>
> Try it.
>
i did, and just like i thought, it doesnt work. why, .innerHTML is executed
*after* document.getElementById("question"), meaning if that returns
nothing; which in this case it does, then there is nothing for innerHTML to
operate on.
i in fact had to wrap the call in an exception just to get it from blowing
up in firefox.
<html>
<head>
</head>
<body>
<p class="question">
Who is Roger Rabbit?
</p>
<script type="text/javascript">
<!--
try {
var oQuestion2 = document.getElementById('question').innerHTML;
} catch(e) { alert(e); }
-->
</script>
</body>
</html>
heres what firefox says on my box:
TypeError: document.getElementById("question") is null
and btw, i think after seeing your solution we can see why i like the xpath
approach so much more :P
-nathan