1

im trying to create a function to help me change some css without having to repeat the code every time.

this is my code:

function error(e){
         e.css({
          'border':'2px solid red',
          'background-color':'rgba(250,52,5,.2)'
         })
}

as you can see i'm adding the (e) parameter, so i can apply the function to any element.

the function works fine but im getting the following error:

Uncaught TypeError: Cannot read property 'css' of undefined at error (scripts1.js:83)

there's any other or a better way to do this? sorry about my english and this simple question, im a begginer as you can see.

Thank you.

4
  • 2
    Use $(e).css(...) Commented Jan 20, 2017 at 1:07
  • What do you pass as e for error? In other words, how do you call error? Commented Jan 20, 2017 at 1:21
  • The problem is in the place where you're calling error(), not in the function itself. You need to show that code. Commented Jan 20, 2017 at 2:06
  • Hi Barmar, thats exactly the problem! Thanks! there was an syntax error when i called the function.. Commented Jan 20, 2017 at 2:20

2 Answers 2

1

It seems you are not passing any value into your function. So you are getting undefined error.

I would suggest for you to setup a class for error in your style sheets like:

.error  {
      border:2px solid red;
      background-color:rgba(250,52,5,.2);
     }

Now whenever you get an error, just set the class on the element:

 function error (e) {
   $(e).addClass('error');
 }

Please ensure that you are passing a Dom element to the error function.

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

Comments

0

Get rid of .css and use .addClass. Define the class in your style sheet.

1 Comment

i'll keep that in mind next time, seems like the simplest way to doit, than you

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.