1

I'm newer to Javascript, so sorry if I'm taking the wrong approach.

I am looking to identify an element on a page by Class Name, hide it, and replace it with the text "Loading..."

Here's what I have so far, which does successfully blank the page, however, my replacement message doesn't show.

<style>
.loading{
display: block;
}
</style>
<script>

if (checkRequiredFields(document.forms[0]) && document.InfoForm.FUDF9.value=="Y1")
{
var elems = document.getElementsByClassName("name");

for (i = 0; i < elems.length; i++) {
    elems[i].style.display = "none";}
}

</script>
<div id = "loading"></div>
<script>
var Message1 = "Loading...";
if (checkRequiredFields(document.forms[0]) && document.InfoForm.FUDF9.value=='Y1'){

document.getElementByClassName("loading").innerHTML = Message1;}
</script>

It is a class name that a lot of elements on the page have, I've tried going to a narrower class name but that doesn't seem to hide the smaller individual elements. Any help would be appreciated.

EDIT: Okay, I reworked everything so it's all operating off an ID. Still confused about why it will blank the page but not show my message.

<style>
#loading{
display: block;
}
</style>
<div id = "loading"></div>
<script>

if (checkRequiredFields(document.forms[0]) && document.InfoForm.FUDF9.value=="Y1")
{
var elems = document.getElementsByClassName("name");

for (i = 0; i < elems.length; i++) {
    elems[i].style.display = "none";}
}

var quickPledgeMessage1 = "Loading...";
if (checkRequiredFields(document.forms[0]) && document.InfoForm.FUDF9.value=='Y1'){

document.getElementById("loading").innerHTML = quickPledgeMessage1;}
</script>
1
  • 1
    The method you seek is getElementsByClassName (note plural), and it returns a NodeList so you'll need something like document.getElementByClassName("loading")[0].innerHTML .... You will not see a refresh until the script finishes, you'll need to split it with setTimeout calls if you want to show something while processing occurs. Commented Apr 11, 2016 at 2:07

2 Answers 2

1

Move your script to the bottom of the page... it is executing before the <div id="loading"></div> is available in the DOM. Or, wrap all the code inside a window.onload callback.

And loading is an ID, use getElementById or better yet use querySelector.

Lastly, the opening { and closing } brackets don't look like they match up.

<style>
.loading{
display: block;
}
</style>
<div id = "loading"></div>
<script>

if (checkRequiredFields(document.forms[0]) && document.InfoForm.FUDF9.value=="Y1")
{
    var elems = document.getElementsByClassName("name");

    for (i = 0; i < elems.length; i++) {
        elems[i].style.display = "none";}
    }
}
var Message1 = "Loading...";
if (checkRequiredFields(document.forms[0]) && document.InfoForm.FUDF9.value=='Y1'){
    document.getElementById("loading").innerHTML = Message1;
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

loading is an ID. So you have two options.

<div id="loading"></div>
document.getElementById("loading").innerHTML = Message1;

or

<div class="loading"></div>
document.getElementsByClassName("loading")[0].innerHTML = Message1;

1 Comment

getElementsByClassName returns a live NodeList, not a single element.

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.