3

I am trying to change the text value of an label inside an li inside an ul.

So:

HTML and JS

document.getElementsByClassName("hs-error-msgs inputs-list").innerText = "Bitte füllen Sie dieses Pflichtfeld aus!";
<ul class="hs-error-msgs inputs-list" style="display:block;" role="alert" data-reactid=".hbspt-forms-0.0:$0.3">
  <li data-reactid=".hbspt-forms-0.0:$0.3.$0"> <label data-reactid=".hbspt-forms-0.0:$0.3.$0.0">Please complete this required field. </label> </li>
</ul>

2
  • 2
    getElementsByClassName returns an Element Collection Commented Feb 7, 2018 at 10:31
  • I know, but how is possible to change the value of the text ? Commented Feb 7, 2018 at 10:33

3 Answers 3

1

getElementsByClassName returns a collection. You could take the first element of this collection and then select label within it with for example another collection method getElementsByTagName.

var ul = document.getElementsByClassName("hs-error-msgs inputs-list")[0]
var label = ul.getElementsByTagName('label')[0]

label.innerText = "Bitte füllen Sie dieses Pflichtfeld aus!";
<ul class="hs-error-msgs inputs-list" style="display:block;" role="alert" data-reactid=".hbspt-forms-0.0:$0.3">
  <li data-reactid=".hbspt-forms-0.0:$0.3.$0"> <label data-reactid=".hbspt-forms-0.0:$0.3.$0.0">Please complete this required field. </label> </li>
</ul>

However way more convenient method for this is querySelector:

var label = document.querySelector(".hs-error-msgs.inputs-list label")

label.innerText = "Bitte füllen Sie dieses Pflichtfeld aus!";
<ul class="hs-error-msgs inputs-list" style="display:block;" role="alert" data-reactid=".hbspt-forms-0.0:$0.3">
  <li data-reactid=".hbspt-forms-0.0:$0.3.$0"> <label data-reactid=".hbspt-forms-0.0:$0.3.$0.0">Please complete this required field. </label> </li>
</ul>

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

2 Comments

Thank you. But now I need to use in the other. How can I use querySelectorAll properly?
@LuisFilipeEscobar querySelectorAll is a collection. So you need to iterate. var labels = document.querySelectorAll(".hs-error-msgs.inputs-list label"); for (var i = 0; i < labels.length; i++) { labels[i].innerHTML = '....' }.
0

You need to query the label, use querySelector

var text = "Bitte füllen Sie dieses Pflichtfeld aus!";
document.querySelector( ".hs-error-msgs inputs-list li label").innerText = text;

Comments

0

Use querySelector to get a single item in one line.

document.querySelector(".hs-error-msgs.inputs-list label").innerText = "Bitte füllen Sie dieses Pflichtfeld aus!";

This will select a single label from inside the element with your specified classes. If you have multiple items, use querySelectorAll and iterate with a for loop.

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.