admin管理员组

文章数量:1302904

When I click on the Click Here!!! I get the two alert() messages but instead of the values BBB and ZZZ that I expect, I get undefined and undefined.

Any ideas what I'm doing wrong? I'm running Firefox 8.0, works in IE 8

<HTML>
<HEAD>
</HEAD>

<BODY>

<div id="1_0">
    <div id='1_1'  style="background-color: yellow;">
        <input="hidden" id="1_a" value="AAA"/>
        <input="hidden" id="1_b" value="BBB"/>
        some text, and some more
        <div>
            <div style="background-color: silver;" onclick="alert(document.getElementById('1_b').value);alert(document.getElementById('1_z').value);">
                Click Here!!!
            </div>
        </div>
    </div>
</div>


<input="hidden" id="1_z" value="ZZZ"/>

</BODY>
</HTML>

When I click on the Click Here!!! I get the two alert() messages but instead of the values BBB and ZZZ that I expect, I get undefined and undefined.

Any ideas what I'm doing wrong? I'm running Firefox 8.0, works in IE 8

<HTML>
<HEAD>
</HEAD>

<BODY>

<div id="1_0">
    <div id='1_1'  style="background-color: yellow;">
        <input="hidden" id="1_a" value="AAA"/>
        <input="hidden" id="1_b" value="BBB"/>
        some text, and some more
        <div>
            <div style="background-color: silver;" onclick="alert(document.getElementById('1_b').value);alert(document.getElementById('1_z').value);">
                Click Here!!!
            </div>
        </div>
    </div>
</div>


<input="hidden" id="1_z" value="ZZZ"/>

</BODY>
</HTML>
Share Improve this question asked Dec 14, 2011 at 16:18 KM.KM. 104k34 gold badges181 silver badges213 bronze badges 0
Add a ment  | 

6 Answers 6

Reset to default 5

problem is with the input=hidden. It should be <input type="hidden">

<HTML>
<HEAD>
</HEAD>

<BODY>

<div id="1_0">
    <div id='1_1'  style="background-color: yellow;">
        <input type="hidden" id="1_a" value="AAA"/>
        <input type="hidden" id="1_b" value="BBB"/>
        some text, and some more
        <div>
            <div style="background-color: silver;" onclick="alert(document.getElementById('1_b').value);alert(document.getElementById('1_z').value);">
                Click Here!!!
            </div>
        </div>
    </div>
</div>


<input type="hidden" id="1_z" value="ZZZ"/>

</BODY>
</HTML>

Looks like an error in your HTML markup on these lines:

<input="hidden" id="1_a" value="AAA"/>
<input="hidden" id="1_b" value="BBB"/>

These need to be:

<input type="hidden" id="1_a" value="AAA"/>
<input type="hidden" id="1_b" value="BBB"/>

Notice the type attributes.

  • Your element definition should be:

    <input type="hidden" .../>
    

    instead of

    <input="hidden" .../>
    
  • Try starting your id with an alpha character (probably isn't the problem, but they should anyway).

First off, your inputs should be <input type="hidden" id="1_a" value="AAA"/> <input type="hidden" id="1_b" value="BBB"/>

<input="hidden"

is wrong, you need:

<input type="hidden"

Working Demo

Because you have a wring markup. instead of

<input="hidden" id="1_a" value="AAA"/>

it should be

<input type="hidden" id="1_a" value="AAA"/>

本文标签: javascriptFirefox not getting value when using documentgetElementById()valueStack Overflow