0

I would like to start by saying I am very new to jquery and javascript I rarely use it, however I now find myself in a position where I need to make use of it.

What I am trying to do

I am trying to let admin-user upload matches to db for a round in a competition, thus building the schedule for round X....hope that makes sense

What should happen

if user selects, as an example, 4 from the dropdown box 8 input fields should be created, thus allowing user to enter the 2 teams which will play in each round in each match.

I have tried to code this, (please dont laugh) but the logic and code is completely wrong, if anyone can be so kind to assist me with this problem it would be much appreciated, possibly allowing me to build from this in the future.

JFiddle

https://jsfiddle.net/leela89/zvss0f8L/#&togetherjs=RApSQ2E6Sr

Code

 <select id="nrGames" name="nrGame">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
    </select>    

<script type="text/javascript">
    //create input element for nr games
    $("#nrGames").change(function(){
     var value = $(this).val;
     var nr = 0;
     while(nr < value){
         $('#games').add('input');
         nr++;
     }
        })

    </script>
<!--APPEND INOUT TEXT -->
    <div id="games">

    </div>
2
  • 1
    I have edited Raja's code and added few lines JSFIDDLE Commented Mar 6, 2016 at 7:22
  • Thank you very much for your time Commented Mar 6, 2016 at 7:25

2 Answers 2

1

Try to .append() the new elements into the target element,

$("#nrGames").change(function() {
  var value = +$(this).val();
  value *= 2;
  var nr = 0;
  var elem = $('#games').empty();
  while (nr < value) {
    elem.append($('<input>',{name : "whateverNameYouWant"}));
    nr++;
  }
});

Also .val() is a function not a property.

DEMO

Sign up to request clarification or add additional context in comments.

6 Comments

Why setting a variable to $('#games').empty() (elem)? Why the + before $(this).val()?
so that program wont break when changing the value of combobox the 2nd time
HI Thank you very much for taking the time to answer the question, if I may ask one more question what will the name attribute be of create elements and how do I set it?
@WcPc 1. That is caching the element, if we call $("#games") inside for loop, performance drop will happen since we are looking up the dom all the time. + will convert the string that we received through .val() into a number.
@Marilee You have to pass the settings as a second parameter. Check the answer now.
|
0

Small update: Different elements for player in each team will be helpful

$("#nrGames").change(function() {
  var value = +$(this).val();
  var nr = 0;
  var elem = $('#games').empty();
  while (nr < value) {
    elem.append($('<input>',{name : "Team1Player"+nr}));
    elem.append($('<input>',{name : "Team2Player"+nr}));
    nr++;
  }
});

Comments

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.