0

How can I populate 50 html5 input fields from an external delimited "|" text file ("players.txt"):

Smith, Bob|Jones, James|Cavanaugh, Harvey|

I have input fields like so:

<input type="text" name = "N01" id = "I01">
<input type="text" name = "N02" id = "I02">

<script>
$jQuery.get('assets/players.txt', function(data) {
splitString = dataString.split("|");

$('#I01').val(splitString[0]);
$('#I02').val(splitString[1]);
$('#I03').val(splitString[2]);

});

</script>
2
  • 1
    change function (data) to function(dataString) Commented Mar 24, 2018 at 22:54
  • and please don't space = Commented Mar 25, 2018 at 1:26

5 Answers 5

1

Try getting html elements using jquery $ sign such as

$('#I01').val(splitString[0]);
$('#I02').val(splitString[1]);
$('#I03').val(splitString[2]);
Sign up to request clarification or add additional context in comments.

Comments

1

You're currently referencing the wrong data variable dataString, instead reference data. Also, if you know your IDs are sequential, you can avoid writing 50 different lines of JS and run a for loop, for instance:

for(i=0; i<splitString.length; i++){
  id = "#I0"+(i+1);
  $(id).val(splitString[i]);
}

Comments

1

Don't set the value of each element individually, use a forEach loop.

Make sure to take into account string padding.

splitString.forEach((str, i) => {
  document.querySelector('#I' + String(i).padStart(2, '0'))
    .value = str;
});

Comments

0

let dataString = "Smith, Bob|Jones, James|Cavanaugh, Harvey|";
let splitString = dataString.split("|");

for (let i = 0; i < splitString.length; i++) {
  $("#I0" + i).val(splitString[i]);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="N01" id="I01">
<input type="text" name="N02" id="I02">

2 Comments

You should factor in what others have suggested, this example only works with the small amount of data provided. Since it hard codes the leading 0. For values >10 look at what CertainPerformance suggested with padStart(2, '0')
Yes, but the smart money is on removing the leading zeros, which I will always do henceforth. Thank for the suggestion.
0

Example without ajax:

$(function(){
  var splitString = 'Smith, Bob|Jones, James|Cavanaugh, Harvey';
  splitString = splitString.split("|");
  $('#playersInputs').empty();
  $.each(splitString, function(i,v){
    $('<input type="text" />')
        .attr('name', 'N'+("0"+(i+1)).slice(-2))
        .attr('id', 'I'+("0"+(i+1)).slice(-2))
        .val(v)
        .appendTo('#playersInputs');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='playersInputs'>
</div>

Example With ajax:

you must replace /path/to/your/text-file with the actual url

$(function(){
  $.get('/path/to/your/text-file', function(data) {
    var splitString = data.split("|");
    $('#playersInputs').empty();
    $.each(splitString, function(i,v){
      $('<input type="text" />')
          .attr('name', 'N'+("0"+(i+1)).slice(-2))
          .attr('id', 'I'+("0"+(i+1)).slice(-2))
          .val(v)
          .appendTo('#playersInputs');
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='playersInputs'>
</div>

4 Comments

This doesn't work with Run code snippet. But I do need an ajax version because the assets/players.txt is generated automatically
@verlager please give me the full url of your text file
Thanks but I fixed it with some php.
@verlager No problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.