0

I have the following piece of code I need in order to update a bootstrap button when a collapsed is opened or closed. However the icon part is updating but impossible to get the button text updated and I have no idea why.

My javascript level is very low and I'm simply using part of code I might find on SOF or anywhere else on internet.

Javascript

$('#more-properties').on('hidden.bs.collapse', function () {
  var icon = document.getElementById("icon-more-properties");
  icon.setAttribute('class', 'fas fa-angle-down pr-2');

  var btn = document.getElementById("btn-more-properties");
  btn.textContent('More properties');
});
$('#more-properties').on('shown.bs.collapse', function () {
  var icon = document.getElementById("icon-more-properties");
  icon.setAttribute('class', 'fas fa-angle-up pr-2');

  var btn = document.getElementById("btn-more-properties");
  btn.textContent('Less properties');
});

Bootstrap button

<div class="col-12 text-center" id="btn-div">
    <button class="btn btn-outline-dark shadow-none collapsed" id="btn-more-properties" type="button" data-toggle="collapse" data-target="#more-properties" style="border-radius: 0 !important;">
    <i id="icon-more-properties" class="fas fa-angle-down pr-2"></i>More properties</button>
</div>

Does anyone can tell me why it is not working ? I can't find any similar issue on iternet.

Thank you in advance for your help.

2
  • Does this link provide you an answer? w3schools.com/bootstrap4/… Commented Oct 1, 2021 at 10:16
  • Are you sure you don't have more than one element in the page with id="btn-more-properties"? If so, that is not allowed. id, at all times and cost, must be unique, per-document. Commented Oct 1, 2021 at 10:44

1 Answer 1

3

that textContent is not a function but a property, so instead of calling it you need to assign the value to it:

btn.textContent = 'Less properties';

Hope this helps 🔥

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

5 Comments

BTW if you're already using jQuery, instead of getElementById and textContent you can simply $("#btn-more-properties").text("...").
@mbojko He's forced to use jQuery because those events are Bootstrap jQuery events. Replacing native API with jQuery in 2021 is definitely a downgrade.
I just tried and indeed it is updating the text thank you, however the text replace also the icon. How should I do to keep it and update the text at the same time ? I was originally thinking about changing the whole html code inside the <div< by pre-writing it in the script and then apply '.html' ; '.innerHtml'vor other but it is simply not working as I expected.
I would suggest for you to wrap the button text on a span and manipulate the span textContent, i.e: <button {buttonProperties}><i {iconProperties}></icon><span>More Properties</span></button> and then from javascript you just get i.e: document.querySelector('#btn-more-properties span').textContent = 'whatever you like' and this way you wont overwrite the icon on button
It worked perfectly thank you !

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.