CodeWalrus

Development => Web => Topic started by: WholeWheatBagels on November 12, 2016, 01:17:31 AM

Title: Troubleshooting a (probably stupid) javascript error
Post by: WholeWheatBagels on November 12, 2016, 01:17:31 AM
So I've been messing around with Javascript lately.
I wrote this to mess with some page code:

javascript:
var divs = document.getElementsByClassName("corrupted-repair-button");
for (var x = 0; x < divs.length; x++) {
    var elem=divs[x];
    elem.removeAttribute('hidden')
}

What I'm trying to do is remove a "hidden" attribute from an element with that class name.
Thing is, it works in the Inspect console,but when I stick it in a bookmark or the onmibar it never works and I have no idea why.

Pretty new to JS so it's probably staring me straight in the face and I haven't caught it yet.
Title: Re: Troubleshooting a (probably stupid) javascript error
Post by: Snektron on November 12, 2016, 09:39:21 AM
But does it work when you paste it in the addres bar? Watcvh out though, Chrome removes the javascript: part when you paste it sop you have to type it manually. Im not sure it it does that with bookmarks too.
Title: Re: Troubleshooting a (probably stupid) javascript error
Post by: ben_g on November 12, 2016, 12:03:27 PM
I don't think bookmarks are supposed to contain multiple lines. Have you tried writing it all on one line like this?

javascript:var divs=document.getElementsByClassName("corrupted-repair-button");for (var x=0;x<divs.length;x++) {divs[x].removeAttribute('hidden');}
Title: Re: Troubleshooting a (probably stupid) javascript error
Post by: WholeWheatBagels on November 13, 2016, 01:05:21 AM
I have tried all of the above to no luck. Bookmarks automatically put everything on the same line, so no problems there; I know they work b/c the image replacer script (BATMAN) works as a bookmark.

Maybe it has something to do with the console needing to be in this mode and not the default "top" mode?

(https://i.imgsafe.org/7bbd99cef6.png)
Title: Re: Troubleshooting a (probably stupid) javascript error
Post by: p2 on November 15, 2016, 11:55:03 AM
in your first post... I think you forgot the ; after the elem.removeAttribute('hidden')
*Will try something out, now ^^

ok this code worked for me:

var divs = document.getElementsByClassName("corrupted-repair-button");
for (var x = 0; x < divs.length; x++) {
  divs[x].style.hidden='';
}



If you have jQuery you can do it a lot eaier by just calling
$('.corrupted-repair-button').style.hidden='';
At least I think so... ^^