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

html - "Uncaught TypeError: x.toUpperCase is not a function" in Javascript - Stack Overflow

programmeradmin1浏览0评论

I have a function to capitalize onchange like this:

<input type="text" id="abc" name="id" onchange="capitalize()">

And function definition is as follows:

<script>
    function capitalize()
    {
        var x= document.getElementById('abc');
        x=x.toUpperCase();
        document.getElementById('abc').value=x;
    }
</script>

I get the error as:

Uncaught TypeError: x.toUpperCase is not a function

What changes should I make or should I include any header files?

I have a function to capitalize onchange like this:

<input type="text" id="abc" name="id" onchange="capitalize()">

And function definition is as follows:

<script>
    function capitalize()
    {
        var x= document.getElementById('abc');
        x=x.toUpperCase();
        document.getElementById('abc').value=x;
    }
</script>

I get the error as:

Uncaught TypeError: x.toUpperCase is not a function

What changes should I make or should I include any header files?

Share Improve this question edited Jul 9, 2015 at 8:55 Vishnu Y S asked Jul 9, 2015 at 8:37 Vishnu Y SVishnu Y S 1936 silver badges19 bronze badges 1
  • possible duplicate of JavaScript get input text value – user663031 Commented Jul 9, 2015 at 8:43
Add a ment  | 

2 Answers 2

Reset to default 3

Use value to get its value and then convert to upper case:

x=x.value.toUpperCase();

You can't make an element into uppercase. You are calling it on the input itself, not the value. If you want the value to bee uppercased then you have to use value:

x= x.value.toUpperCase();
发布评论

评论列表(0)

  1. 暂无评论