Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle change text of span

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?

like image 392
Arian Avatar asked Jun 09 '12 05:06

Arian


People also ask

How do I change the text inside a 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.

How would you update the text written inside a span element with?

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.

How do you text the span of an element?

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.

How do you set span value?

jQuery: Set a value in a span Set a value in a span using jQuery. JavaScript Code: $(document). ready(function(){ $('#button1').


1 Answers

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');  
like image 165
Ruchit Patel Avatar answered Oct 07 '22 03:10

Ruchit Patel