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/)

Community
  • 1
  • 1
Kristin P
  • 35
  • 1
  • 6
  • No need to escape `"`, since they are enclosed using `'`. Also, do you have jQuery included before this script appears? – Enstage May 21 '17 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. – Michael Coker May 21 '17 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? – deceze May 21 '17 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. – Kristin P May 22 '17 at 00: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. – Kristin P May 22 '17 at 00:02

2 Answers2

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>' ?>
Michael Coker
  • 52,626
  • 5
  • 64
  • 64
0

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

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

yezzz
  • 2,990
  • 1
  • 10
  • 19
  • 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. – Kristin P May 22 '17 at 00:09
  • Ít probably was due to escaping, but I see you got it fixed. – yezzz May 22 '17 at 10:07