I'm using jQuery and I want to show some calculation in a span (called span1
) and I want when text of span1
changed do some calculation on it's value and show in other spans (called `span2 ,span3,...). How I can handle text change of span?
Use the textContent property to change the text of a span element, e.g. span. textContent = 'Replacement text' . The textContent property will set the text of the span to the provided string, replacing any of the existing content.
Use innerText is the best method. As you can see in the MDM Docs innerText is the best way to retrieve and change the text of a <span> HTML element via Javascript.
Use the textContent property to get the text of a span element, e.g. const text = span. textContent . The textContent property will return the text content of the span and its descendants.
jQuery: Set a value in a span Set a value in a span using jQuery. JavaScript Code: $(document). ready(function(){ $('#button1').
Span does not have 'change' event by default. But you can add this event manually.
Listen to the change event of span.
$("#span1").on('change',function(){ //Do calculation and change value of other span2,span3 here $("#span2").text('calculated value'); });
And wherever you change the text in span1. Trigger the change event manually.
$("#span1").text('test').trigger('change');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With