0
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
    {
    }
}   
0

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

means instead of temid i have to use document.getElementById(id).value;??
See that document.getElementById(id) it just gives you the object. The value of the object can be retrieved using value property. Now since it looks that you want to work on the value. Yes you will have to use it that way. Things can be simple if you try alerting what you are getting in your variables. like alert(tempid) with and without value.
1

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
    {

    }
} 

Comments

0
    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
       {

       }

    }   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.