user1041240
user1041240

Reputation: 73

Object doesnt support this property or method in javascript

function add(id)
{
    var tempid=document.getElementById(id);
    var patterm=/@/;
    var value=tempid.match(patterm);  // This is where I'm getting the error
    if(value==null)
    {
        var length=document.getElementById(id).length();

        tempid=tempid.setchatAt(length+1,'@messung.com');
    }
    else
    {
    }
}   

Upvotes: 0

Views: 254

Answers (3)

Sandeep Pathak
Sandeep Pathak

Reputation: 10747

    function add(id)
    {
       var tempid=document.getElementById(id);
       var patterm=/@/;
       var value=tempid.value.match(patterm);  // use value property of the Dom Object
       if(value==null)
       {
          var length=tempid.value.length(); //Call  lenght on the value of object 

          tempid.value = tempid.value.setchatAt(length+1,'@messung.com'); //set proper value
       }
       else
       {

       }

    }   

Upvotes: 0

jfriend00
jfriend00

Reputation: 707238

On this line, you are trying to do a string match on a DOM object which will never work.

var value=tempid.match(patterm);

That is probably not what you mean to be doing. If this is an input field (it looks like you're testing for a '@' in an email address), then you would need to get the value of the input field, not just the DOM object. It's also inefficient to use a regular expression to search for one character in a string. Here's a cleaned up version of your function:

function add(id)
{
    var val = document.getElementById(id).value;
    // if no '@' in string, add default email domain onto the end
    if (val.indexOf('@') == -1)
    {
         val += '@messung.com';
    }
    else
    {

    }
} 

Upvotes: 1

sushil bharwani
sushil bharwani

Reputation: 30187

the tempid is an object you need to match its value to the pattern. Do something like document.getElementById(id).value;

Also length is a property rather than a method. And again it needs to be called on document.getElementById(id).value; that is the string. Not on the object.

Upvotes: 1

Related Questions