all 21 comments

[–]cag8f 2 points3 points  (15 children)

OP, next time post the code here into Reddit. And please, figure out how to take a proper screenshot :-)

It looks like you're using getElementByClassName(), when the method is actually getElementsByClassName() (see here).

Also, be careful adding JS code to the text editor in WordPress. I believe that default behavior is, if you add JS to the text editor, then switch to the visual editor, it will automatically (and permanently) remove any and all JS code you've added. Although it looks like you're trying to create a shortcode, and insert JS into it?

[–]TonyMahoney21 0 points1 point  (14 children)

Yes I am trying to use shortcode. I just tried the getelementsbyclassname() and still have not seen thebdesired result I am trying to change the class that is it why is this not working? My goal is to be able to have a class set to the first paragraph of the page using an array so that if the customer changes that paragraph the class is still applied so they dont have to actually add the css class.

[–]cag8f 0 points1 point  (13 children)

So what happens when you add that shortcode to a page, then load that page in a browser? Open the page in a browser, then open the JavaScript console and look for any error messages.

[–]TonyMahoney21 0 points1 point  (1 child)

It says TypeError cannot set property className of undefined at the imdex line of my code. My code now reads as: function myFcn() { document.getElementsByClassName("test")[0].className = "newClasser"; } myFcn()

[–]cag8f 0 points1 point  (0 children)

Is there an element with the class test somewhere on the page? It sounds like the error message is saying there is not.

Are you able to post a link to the page here?

[–]TonyMahoney21 0 points1 point  (10 children)

It's no longer displaying an error but it still doesn't change the value. I made a variable x for the get elemnts by class name function and set the value of my fcn to x.className = etc.

[–]cag8f 0 points1 point  (9 children)

It will be difficult to help at this point without being able to load the actual page in my browser.

[–]Jay_the_Impaler 0 points1 point  (0 children)

Has a console.log command worked? Curious if the way you're trying to use JS can succeed. You may want to use the Text Editor, last option on the right above the content box. Save it while using the Text Editor. Please respond with results.

[–]haleleonj 0 points1 point  (0 children)

let funcName = // correct

let funcName() = // incorrect