html - Javascript: this.value Function Not Working - Stack Overflow

Ok so I have the following function:function hideValue(value) {if (this.value == value) {this.value = &

Ok so I have the following function:

function hideValue(value) {
    if (this.value == value) {
       this.value = '';
}

And the following form field:

<input onfocus="hideValue('First Name')" type="text" name="first_name" value="First Name">

I cannot get the function to hide the value. When I alert(value); in the function, it returns the correct value.

I also have a showValue function which does the opposite. Why are these not working?

Ok so I have the following function:

function hideValue(value) {
    if (this.value == value) {
       this.value = '';
}

And the following form field:

<input onfocus="hideValue('First Name')" type="text" name="first_name" value="First Name">

I cannot get the function to hide the value. When I alert(value); in the function, it returns the correct value.

I also have a showValue function which does the opposite. Why are these not working?

Share Improve this question asked Apr 21, 2016 at 5:43 Timothy FisherTimothy Fisher 1,12914 silver badges34 bronze badges 3
  • 1 this doesn't refer to the element when you call the function that way (so this.value is likely undefined). – nnnnnn Commented Apr 21, 2016 at 5:46
  • Note, you're missing a closing bracket which will give you a syntax error. Answerers should be aware of this in their responses. – Andy Commented Apr 21, 2016 at 5:52
  • Woops that was my bad. It's in my code but not in the post. Thanks to everyone who responded, makes sense now! – Timothy Fisher Commented Apr 21, 2016 at 5:53
Add a ment  | 

3 Answers 3

Reset to default 5

In an event handler on a DOM element this refers to the element only in the first level of the function. Therefore you need to pass this into the function:

<input
  onfocus="hideValue(this,'First Name') /* 'this' is only correct here */"
  type="text" name="first_name" value="First Name"
>

The function should therefore be modified to:

function hideValue(element, value) {
  if (element.value == value) {
    element.value = '';
  }
}

I'll remend to use placeholder for this kind of functionality.

<input type="text" name="first_name" placeholder="First Name" />
                                     ^^^^^^^^^^^^^^^^^^^^^^^^

Browser support

Demo:

<input type="text" name="first_name" placeholder="First Name" />


Problem:

this inside the function hideValue() refers to the Global window object.

Solution:

You can pass this reference to the event handler.

<input onfocus="hideValue(this, 'First Name')"
                          ^^^^

And catch that inside event handler

function hideValue(that, value) {
                   ^^^^           // What is that? `this`
    if (that.value === value) {
        that.value = '';
    }
}

Demo

function hideValue(that, value) {
    if (that.value === value) {
        that.value = '';
    }
}
<input onfocus="hideValue(this, 'First Name')" type="text" name="first_name" value="First Name">

<input onfocus="hideValue(this,'First Name')" type="text" name="first_name" value="First Name">

<script>

    function hideValue(thisvalue,value) {

    if (thisvalue.value == value) {
       thisvalue.value = '';
        }
    }
</script>

Please check this. Its working fine

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742373797a4431784.html

相关推荐

  • html - Javascript: this.value Function Not Working - Stack Overflow

    Ok so I have the following function:function hideValue(value) {if (this.value == value) {this.value = &

    14小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信