admin管理员组文章数量:1318958
Following is my HTML code
<div id="dl_address" class="dl-row">
<div class="dl-col1">
<span id="dl_addressMarker" class="dl-Marker">*</span>Address:
</div>
<div class="dl-col2">
<input id="dl_addressMarker" name="p.streetAddress" maxlength="100" size="15"
class="landing-qualpanel-input-standin" type="text" onClick="formOpt(id)" value="Home Address">
</div>
</div>
And Following is my javascript code
<script language="javascript" type="text/javascript">
function formOpt(x){
var y=document.getElementById(x).value;
alert(y);
}
</script>
I can't understand why i am getting Undefined. Kindly Help. Thanks in advance
Following is my HTML code
<div id="dl_address" class="dl-row">
<div class="dl-col1">
<span id="dl_addressMarker" class="dl-Marker">*</span>Address:
</div>
<div class="dl-col2">
<input id="dl_addressMarker" name="p.streetAddress" maxlength="100" size="15"
class="landing-qualpanel-input-standin" type="text" onClick="formOpt(id)" value="Home Address">
</div>
</div>
And Following is my javascript code
<script language="javascript" type="text/javascript">
function formOpt(x){
var y=document.getElementById(x).value;
alert(y);
}
</script>
I can't understand why i am getting Undefined. Kindly Help. Thanks in advance
Share Improve this question asked Feb 3, 2013 at 11:50 talhaMalik22talhaMalik22 1554 gold badges5 silver badges13 bronze badges 1-
For starters you have two different elements with the same ID, which one were you expecting
getElementById
to return? – Ian Roberts Commented Feb 3, 2013 at 11:54
3 Answers
Reset to default 3That's because you have two elements with the same Id, which isn't allowed in HTML and which makes it impossible for your function to get the input this way.
Change the id of your input and you'll be fine.
You could simplify the code by passing the element and not the id :
HTML :
<input id="dl_addressMarker2" name="p.streetAddress"
maxlength="100" size="15"
class="landing-qualpanel-input-standin" type="text"
onClick="formOpt(this)" value="Home Address">
JavaScript :
function formOpt(x){
var y = x.value;
alert(y);
}
You have 2 problems. (1) you should call your method like this: formOpt(this)
which will pass the current element in. (2) You have repeated DOM ids.
Write is like this:
<script language="javascript" type="text/javascript">
function formOpt(x){
//x is now the input element. You can fetch its id property.
var y=document.getElementById(x.id).value;
// you can do other things with x also.
alert(y);
}
</script>
<div id="dl_address" class="dl-row">
<div class="dl-col1">
<span id="dl_addressMarker" class="dl-Marker">*</span>Address:
</div>
<div class="dl-col2">
<!-- I renamed the id on the input -->
<input id="dl_addressMarker_input" name="p.streetAddress" maxlength="100"
size="15" class="landing-qualpanel-input-standin"
<!-- pass this into the function -->
type="text" onClick="formOpt(this);" value="Home Address">
</div>
</div>
See working sample: http://jsfiddle/KXCeh/1/
you need to pass the value of id, and you also need to keep the ids unique for each and every element
<div class="dl-col2">
<input id="dl_addressMarker" name="p.streetAddress" maxlength="100" size="15"
class="landing-qualpanel-input-standin" type="text" onClick="formOpt('dl_addressMarker')" value="Home Address">
</div>
本文标签: javascriptDocumentgetElementByIdvalue returning UndefinedStack Overflow
版权声明:本文标题:javascript - Document.getElementById.value returning Undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742057407a2418377.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论