using "this" inside a function?

Discussion in 'JavaScript' started by mahmood, Jul 31, 2007.

  1. #1
    I have a button that upon clicking calls a function. I want to make this button disabled inside the function without refereing to its id so I tried to use this.disabled=true; but it doesn't seem to be working.

    Can't I use "this" to refer to the object that calls a function inside the function?
     
    mahmood, Jul 31, 2007 IP
  2. disco_danny

    disco_danny Peon

    Messages:
    12
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    When calling the function you can pass "this" and the element where the javascript occurs will be passed.

    The example below works in both ie and firefox:

    <script language="javascript">
    function testThis(obj)
    {
    obj.disabled = true;
    }
    </script>

    <input type="button" id="btnDisable" name="btnDisable" value="Disable" onClick="testThis(this);"/>
     
    disco_danny, Jul 31, 2007 IP
    mahmood likes this.
  3. mahmood

    mahmood Guest

    Messages:
    1,228
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks disco_danny.:)
     
    mahmood, Jul 31, 2007 IP