2

This is basically a follow up to my yesterday's question.

I managed to get a span perfectly on top my my input text (the span should be replicating the input text).

Since there doesn't seem to be a good way of hiding the original input text, I decided to just update the span rapidly enough to make it seamless, like this:

var updatespan = function(){
    document.getElementById('street1span').innerHTML = document.getElementById('street_1').value.substring(0, 15) + '<span style="color: red;">' + document.getElementById('street_1').value.substring(15) + '</span>';
  };

    $('street_1').on('focus', function(e){
      interval1 = setInterval(updatespan, 10);
    });  

Now I am wondering how resource intensive updating a span every 10ms would be? (I do clear the interval on blur)

I know JQuery is client side, but I just have a hard time putting things into perspective.

Of course, if there is a better way of doing this I would be happy to hear.

Community
  • 1
  • 1
Metal Mathematician
  • 388
  • 1
  • 3
  • 15

1 Answers1

1

If you're getting the value of the input field, you better just use onchange or oninput event that will modify the span element attribute everytime the input field content is changed, instead of using setInterval.

document.getElementById('street_1').oninput = function(){ updatespan(); }
Abana Clara
  • 4,602
  • 3
  • 18
  • 31