1

I am trying to create mcq question with dynamic id by using jquery. but i got stuck when i am defining a unique id for the option of each question. can somebody help me to find way to create unique id for each mcq option? below is my jquery

<html>
<head>
<script type="text/javascript" src="jquery-1.10.2.min.js"></script>
</head>

<body>
<script type="text/javascript">

$(document).ready(function(){
var counter = 2;//question

$("#addQuestion").click(function () {

var newTextBoxDiv = $(document.createElement('div'))
     .attr("id", 'TextBoxDiv' + counter);

newTextBoxDiv.after().html('<label>Q'+ counter + ' : </label>' +
      '<input type="text" name="textbox' + counter + 
      '" id="textbox' + counter + '" value="" >' + '</br>' + 
      '<label>A. </label>'+
      '<input type="text" id="Q'+counter+'choice1">' +
      '<input type="button" value="Add Option" id="'+ counter + '">');

newTextBoxDiv.appendTo("#TextBoxesGroup");


counter++;
 });

 $("#removeQuestion").click(function () {
if(counter==1){
      alert("No question that can be deleted");
      return false;
   }   

counter--;

    $("#TextBoxDiv" + counter).remove();

 });

 //$("#getButtonValue").click(function () {

// var msg = '';
//for(i=1; i<counter; i++){
 // msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
//}
//      alert(msg);
 //});
});
</script>
</head><body>

here is the html

<div id='TextBoxesGroup'>
<div id="TextBoxDiv1">
    <label>Q1 : </label><input type='textbox' id='textbox1' ></br>
    <label>A. <input type='text' id='Q1choice1'>
    <input type='button' value='Add Option' id='1'>
</div>
</div>
<input type='button' value='Add Question' id='addQuestion'>
<input type='button' value='Remove Question' id='removeQuestion'>

</body>
</html>
2
  • @user2310289 i got stuck in creating dynamic id for the mcq option. Commented Nov 14, 2013 at 8:18
  • I don't see what is wrong - counter is a global variable Commented Nov 14, 2013 at 8:20

3 Answers 3

1

You have to define the var counter before document.ready

Does not work: 
$(document).ready(function () {
        var counter = 2;//question



  Works:
var counter = 2;//question
$(document).ready(function () {

EDIT: counter is only global is defined outside $(document).ready

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

Comments

0

Try append newTextBoxDiv in your code:

   .... var newTextBoxDiv = $(document.createElement('div'))...
         .attr("id", 'TextBoxDiv' + counter);
    $('body').append(newTextBoxDiv);
  ...  newTextBoxDiv.after().html('<label>Q'....

$('body').append(newTextBoxDiv);

Comments

0

try something like this

  var counter = $("#TextBoxesGroup div").length;

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.