0

Hi I am trying to update the " Your submission was successful."of following script, with following javascript code :

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>

Its replacing the text along with button class. Can i replace only the text ?

2

2 Answers 2

2

The problem with updating inner html of animate-in is there is a button element inside that element which you don't want to change.

So you need to access the first child of animate-in which is the text node to be updated.

document.querySelector("#global-alert-queue .animate-in").firstChild.nodeValue = "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>


Or a better solution will be to wrap the text in another element and update that 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>

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

Comments

2

Just do this:

Html

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

Javascript

function changeText() {
    document.getElementsByClassName("animate-in")[0].firstChild.nodeValue = "Change Text";
}

Here the demo: http://jsfiddle.net/v9fmzLpx/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.