最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How do I get the id of a textbox that is invoking a javascript function? - Stack Overflow

programmeradmin0浏览0评论

What I want is to get the id (i.e myHeader, myHeader2, etc.)

This is what I'm trying now,

HTML:

<input type="text" id="myHeader" onblur="getValue(this)" />
<input type="text" id="myHeader2" onblur="getValue(this)" />
<input type="text" id="myHeader3" onblur="getValue(this)" />
<input type="text" id="myHeader4" onblur="getValue(this)" />

Javascript:

function getValue(obj){
    var someVar = document.getElementById(obj).value;
}

But I'm not getting anything, the event is not firing as well, how do I do this?

What I want is to get the id (i.e myHeader, myHeader2, etc.)

This is what I'm trying now,

HTML:

<input type="text" id="myHeader" onblur="getValue(this)" />
<input type="text" id="myHeader2" onblur="getValue(this)" />
<input type="text" id="myHeader3" onblur="getValue(this)" />
<input type="text" id="myHeader4" onblur="getValue(this)" />

Javascript:

function getValue(obj){
    var someVar = document.getElementById(obj).value;
}

But I'm not getting anything, the event is not firing as well, how do I do this?

Share Improve this question asked Jun 27, 2012 at 10:34 user1485327user1485327 111 gold badge1 silver badge2 bronze badges 3
  • Why would you need to? You already have obj that should refer to the element, selecting it again is pointless. – Anthony Grist Commented Jun 27, 2012 at 10:36
  • <input type="text" id="myHeader" onblur="getValue('myHeader')" /> – onemach Commented Jun 27, 2012 at 10:37
  • @onemach That's hardly a generic solution; you'd have to hardcode the function calls with the id on every single element. – Anthony Grist Commented Jun 27, 2012 at 10:37
Add a ment  | 

5 Answers 5

Reset to default 5

Your obj object already have id and value property:

function getValue(obj){
    alert('id - ' + obj.id);
    alert('value - ' + obj.value);
}

Note that when you pass this context to your function, you pass your DOM element. Not id. And you can operate with it already as with DOM element. No need for document.getElementById.

function value(object)
 {
alert('id= ' + object.id);
 }

As I see it, you might be firing the function when user loses focus on that input field.

Unless you are doing obj.value.toUpperCase(); why do you want to fire the same function on each input field?

Anyways as @antyrat said this would give you the DOM element and you can pick value from it.

 function getValue(obj){
 obj.value=obj.value.toUpperCase();
 }

this would make all for of your input fields to upper case as user keeps moving ahead.

  <script type="text/javascript">
    function getValue(obj) {
        var id = obj.id;
        var value = obj.value;
    }
</script>

Also you can get the textbox id

<input type="text" id="myHeader" onblur="getValue(this.id)" /> <!-- instead of getValue(this) -->

<script type="text/javascript">
    function getValue(id) {
        alert(id);  // Display the id of the textbox
    }
</script>

Hope it will help you.

发布评论

评论列表(0)

  1. 暂无评论