0

I'm trying to remove a class of a certain element (#main-content) on a single page using Javascript. The page was written in PHP, but from what I can find, Javascript is the easiest method for removing the class. I referenced the post: How can I insert javascript in php? and RemoveClass AddClass When Page Load to help me come up with the following solution:

<? php echo '<script type=\"text/javascript\">
        $(document).ready(function() {
         $(\"section#main-content\").removeClass(\"trans-header\");
        });</script>';
?>

I validated this code and placed it at the top of my page but it doesn't seem to work. What might I be missing? (Also if it helps, here is the page I am trying to apply the code to: http://7a9.007.myftpupload.com/contact/)

5
  • No need to escape ", since they are enclosed using '. Also, do you have jQuery included before this script appears? Commented May 21, 2017 at 23:51
  • did you look to see where it's adding the js? It's putting it before html and you're escaping the " when you don't need to like @Enstage mentioned. Commented May 21, 2017 at 23:56
  • 2
    If the page is generated in PHP, why don't you alter the PHP to simply not include the class in the first place, instead of fighting a Javascript battle with your own site within the user's browser? Commented May 21, 2017 at 23:56
  • I can see Jquery is being loaded on page just by inspecting it but I'm not sure from where. I am modifying a theme another developer created so I'm not sure how to tell where it is being loaded from... I assumed it was pulling from the header() ? The page I'm modifying is a template that is referencing a separate header file with the class. I only want this template/page with the header to be modified not every page with the header. Commented May 22, 2017 at 0:01
  • I tried it without the \ escaping and I kept getting an error to say that it was expecting a "," or a ";" in my code. Commented May 22, 2017 at 0:02

2 Answers 2

1

Use this instead, you don't need jQuery. And add this either somewhere in <head></head> or within <body></body> - not above html as it is currently.

<?php echo '<script>document.getElementById("main-content").classList.remove("trans-header");</script>' ?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. I placed this at the bottom just before the footer and it works! I wasn't understanding where the proper placement was for this.
@KristinP awesome, no problem :)
0

It's a wordpress site... replace all $ with jQuery.

Also, check your console to see there's another error on the page.

2 Comments

Thank you. I tried to replace with jQuery but that did not seem to work. I did see there was another error on the page, but I'm not sure how to address that either. Something built-in to the theme code I suspect.
Ít probably was due to escaping, but I see you got it fixed.

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.