Fedor Alexandrovich
Fedor Alexandrovich

Reputation: 147

adding some javascript variable into input's VALUE attribute

Got some js animated dots that work fine as text,

var dots = 0;

$(document).ready(function()
{
    setInterval (type, 600);
});

function type()
{
    if(dots < 3)
    {
        $('#dots').append('.');
        dots++;
    }
    else
    {
        $('#dots').html('');
        dots = 0;
    }
}

but can not use it in the VALUE attribute of my input button (after "processing").

<input type="button" value="Pay" onClick="this.disabled=true; this.value='Processing'">

How to insert it in the right way? http://jsfiddle.net/te58dadw/2/

Upvotes: 0

Views: 586

Answers (3)

adeneo
adeneo

Reputation: 318302

You'd use jQuery's val() to set the value, and with a callback it's easy to append to the value.

A small plugin that handles this could be useful, something like

$.fn.dots = function(time, dots) {
  return this.each(function(i,el) {
    clearInterval( $(el).data('dots') );

    if ( time !== 0 ) {
      var d = 0;
      $(el).data('dots', setInterval(function() {
        $(el).val(function(_,v) {
          if (d < dots) {
            d++;
            return v + '.';
          } else {
            d = 0;
            return v.substring(0, v.length - dots)
          }
        })
      }, time));
    }
  });
}

Which you'd call like

$('.elements').dots(600, 3);

and can be cancelled by just passing zero

$('.elements').dots(0);

Here's a demonstration showing how easy it is to use.

$.fn.dots = function(time, dots) {
  return this.each(function(i,el) {
    clearInterval( $(el).data('dots') );
    
    if ( time !== 0 ) {
      var d = 0;
      $(el).data('dots', setInterval(function() {
        $(el).val(function(_,v) {
          if (d < dots) {
            d++;
            return v + '.';
          } else {
            d = 0;
            return v.substring(0, v.length - dots)
          }
        })
      }, time));
    }
  });
}

$(document).ready(function() {
  $('#dots').on('click', function() {
    $(this).val('Proccessing').prop('disabled',true).dots(600, 3);
    
    $('#cancel').show();
  });
  
  $('#cancel').on('click', function() {
    $(this).dots(200, 10);
    $('#dots').dots(0);
    setTimeout(function() {
      $('#dots').prop('disabled', false).val('Pay');
      $('#cancel').hide().dots(0);
    },2000);
  }).hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="dots" type="button" value="Pay" />
<br /><br />
<input id="cancel" type="button" value="Cancel dots" />

Upvotes: 0

guest271314
guest271314

Reputation: 1

<input> element does not have .innerHTML property which is displayed. .append() and .html() set an elements' .innerHTML property, not .value property.

<input> element has .value property which can be set at jQuery using .val(function)

var dots = 0;
var dot = ".";

$(document).ready(function() {
  setInterval(type, 600);
});

function type() {
  if (dots < 3) {
    $('input').val(function(i, val) {
      return val + dot
    });
    dot.concat(".");
    dots++;
  } else {
    $('input').val("Pay");
    dots = 0;
    dot = "."
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
HERE IT WORKS
<div>processing<span id="dots"></span></div>
<BR>
<BR> HERE IT DOESN"T
<BR>
<input type="button" value="Pay" onClick="this.disabled=true; this.value='Processing'">

Upvotes: 0

ewwink
ewwink

Reputation: 19164

in jQuery use .attr('value', '.....') or .val()

var dots = 0;

$(document).ready(function() {
  $('#payDots').on('click', function() {
    $(this).attr('disabled', 'disabled');
    setInterval(type, 600);
  })

});

function type() {
  var dot = '.';
  if(dots < 3) {
    $('#payDots').val('processing' + dot.repeat(dots));
    dots++;
  }
  else {
    $('#payDots').val('processing');
    dots = 0;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input id="payDots" type="button" value="Pay">

Upvotes: 2

Related Questions