I need a sequential generator in javascript

Asked

Viewed 1,959 times

1

I need a code that I put the URL and number of repetitions in the input and when to click to generate the sequence exit like this in the textarea:

rede[0]="http://www.siteescolhidoaqui"; rede[1]="http://www.siteescolhidoaqui"; rede[2]="http://www.siteescolhidoaqui"; rede[3]="http://www.siteescolhidoaqui"; rede[4]="http://www.siteescolhidoaqui"; rede[5]="http://www.siteescolhidoaqui"; 

this way up starting at zero until the number chosen by me and always the same url chosen... javascript and a mini html form to put the URL and the number of repetitions... I believe it’s easy, someone can help me?

I tried the one that Renan commented on and I got:

<script>
(function() {

  document.querySelector('button').addEventListener('click', toTextarea);

  function toTextarea() {
    var $urlInput = document.querySelector('input[type="url"]'),
        url       = $urlInput.value,
        inputName = $urlInput.getAttribute('name'),
        repeat    = document.querySelector('input[type="number"]').value;

    if (url && repeat) {

      var output = '';
      for (var i = 0; i < repeat; i++)
        output += inputName + '[' + i + ']="' + url + '"; ';
      document.querySelector('textarea').textContent = output;
    }
  }
})();
</script>
<input placeholder='URL' type='url' name='rede'>
<input placeholder='Sequencia' type='number'>
<button>Ir</button>

<textarea placeholder='Resultado'></textarea>

1 answer

3


It is unclear whether you should consider the index or the number of elements. For example, 5 repetitions based on the quantity:

[0][1][2][3][4] = 5 elementos.

Or, 5 repetitions considering the index:

[0][1][2][3][4][5] = 6 elementos.

In case it is the second, just change the below loop to go from 0 until i be it <= the value of repetitions:

(function() {

  document.querySelector('button').addEventListener('click', toTextarea);

  function toTextarea() {
    var $urlInput = document.querySelector('input[type="url"]'),
        url       = $urlInput.value,
        inputName = $urlInput.getAttribute('name'),
        repeat    = document.querySelector('input[type="number"]').value;

    if (url && repeat) {

      var output = '';
      for (var i = 0; i < repeat; i++)
        output += inputName + '[' + i + ']="' + url + '"; ';
      document.querySelector('textarea').textContent = output;
    }
  }
})();
<input placeholder='URL' type='url' name='rede'>
<input placeholder='Sequência' type='number'>
<button>Ir</button>

<textarea placeholder='Resultado'></textarea>

  • That’s exactly what it looks like, but it’s not working out here, so when I click go nothing comes out of the result

  • Here where? I tested in Firefox, Chrome, IE and Edge, in all of them is working.

  • I use Firefox and Opera to send you an email if you’re right please

Browser other questions tagged

You are not signed in. Login or sign up in order to post.