Change the css of an element by id

Asked

Viewed 145 times

-2

I tried the following code:

$("#maintenanceRequest").css({"backgroundColor": "black"});

But the following error appears:

Uncaught TypeError: $(...).css is not a function
    at <anonymous>:1:26

What am I doing wrong? Thank you

  • Bruna, the problem may be in another part of the code. The way to change the CSS of the element is correct and should work.

  • jQuery is on the page? , is it available at the time you execute this code? , I ask these questions because even though Query had not found the selector the function .css would still be available.

1 answer

1


From what I see, you are trying to implement css in a jquery, which is not necessary and I advise you to use a separate project style sheet for better organization of your code.

Possible solutions:

If adhere to using a stylesheet, it would be like this:

#maintenanceRequest{
   background-color: black;
}

or if you want to style only in content (a <div> for example) just use the tag <style> that would look like EX.:

<div style="background-color: black;"></div>

NOTE: be more specific when detailing your question/problem because as you posted it is difficult to identify where exactly is the problem, I suggest edit your question, I answered because the problem is simple and did not need to be something so specific.

I hope I may have helped in some way :)

  • The problem is if the purpose is to change the CSS dynamically, which is very common.

  • truth so I asked her to edit the question, and say clearly, what she really wants to do

Browser other questions tagged

You are not signed in. Login or sign up in order to post.