0

Hi I am trying to update the innerHTML of following script

<div id="global-alert-queue" class="layout-wrapper">
<div class="alert success animate-in" role="alert">
Your submission was successful.
<button id="dismiss-alert" class="dismiss" type="button"></button>
</div>
</div>

I have tried following

var cls =document.getElementById("global-alert-queue").getElementsByClassName("animate-in")[0].innerHTML = "Change Text";

But its not working.

10
  • jsfiddle.net/arunpjohny/mckgxkr1/1 - looks fine Commented Jun 15, 2015 at 12:53
  • 1
    any error in your browser console Commented Jun 15, 2015 at 12:53
  • 1
    make sure the document is ready Commented Jun 15, 2015 at 12:54
  • No. But if i try to print cls its printing properly. Commented Jun 15, 2015 at 12:55
  • Try this: jsfiddle.net/tusharj/kg3cc4gw/3 Commented Jun 15, 2015 at 12:56

5 Answers 5

1

See the comments inline:

// Run when document is completely rendered and is ready to be manipulated 
document.addEventListener("DOMContentLoaded", function () {
    document.querySelector("#global-alert-queue .animate-in").innerHTML = "Change Text";
});

Demo: http://jsfiddle.net/tusharj/kg3cc4gw/3/

querySelector

Returns the first element within the document (using depth-first pre-order traversal of the document's nodes) that matches the specified group of selectors.

More on querySelector: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector

Sign up to request clarification or add additional context in comments.

Comments

0

seems like it is working correctly. Maybe you should move your script below the HTML. or check for document ready.

 var cls =document.getElementById("global-alert-queue").getElementsByClassName("animate-in")[0].innerHTML = "Change Text"; 
<div id="global-alert-queue" class="layout-wrapper">
<div class="alert success animate-in" role="alert">
Your submission was successful.
<button id="dismiss-alert" class="dismiss" type="button"></button>
</div>
</div>

1 Comment

Yeah it was working but its removing the button class. Can i change the text without removing the button class ?
0

You need to remove the var cls=.... Here's a working jsFiddle.

1 Comment

Yeah it was working but its removing the button class. Can i change the text without removing the button class ?
0

As per your updated requirement(Expected is "Change Text" should replace "Your submission was successful.")

You cannot update the inner html of animate-in as it will remove the button element also.

So one solution will be is to update only the first child of animate-in which is the text node containing the value like

document.querySelector("#global-alert-queue .animate-in").firstChild.nodeValue = "Change Text";

Another way is to update the markup to wrap the text in another element like

document.querySelector("#global-alert-queue .animate-in > span").innerHTML = "Change Text";
<div id="global-alert-queue" class="layout-wrapper">
  <div class="alert success animate-in" role="alert">
    <span>Your submission was successful.</span>
    <button id="dismiss-alert" class="dismiss" type="button"></button>
  </div>
</div>

Comments

0

you can add button as string with Text then you get your Output Demo Html

function changeText() {

  var cls = document.getElementById("global-alert-queue").getElementsByClassName("animate-in")[0].innerHTML = 'Change Text <button id="dismiss-alert" class="dismiss" type="button" onclick="changeText()">Change</button>';
}
<div id="global-alert-queue" class="layout-wrapper">
  <div class="alert success animate-in" role="alert">
    Your submission was successful.
    <button id="dismiss-alert" class="dismiss" type="button" onclick="changeText()">Change</button>
  </div>
</div>

Comments

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.