-1

I am trying to build an online exam system. To make question paper i am creating two types of input fields dynamically. One is for question, and another is for options of the question. My jquery codes

<div id="q_paper"></div>
<br/><button type="button" id="go2">Ok</button>

for(var i=0;i<total_question;i++){
                var nw="<div class='q'>("+(i+1)+".)<textarea class='text_area'>Question"+(i+1)+"</textarea><br/>";
                for(var j=0;j<option_number;j++){
                    nw+="<input type='radio'/><input type='text' class='opt' value='option"+(j+1)+"' onfocus='this.value="+null+"'/>"+'<br/>';
                }
                nw+="</div>";
                $("#q_paper").append(nw);
            }

            $(".text_area").click(function(){
                $(this).text("");
            });

But i cann't access the input text from the both input field fields. I have tried a number of times but failed. How to get input text when a ok button is clicked ?

6
  • whats your code to get the text Commented May 1, 2015 at 18:43
  • How are you trying to access the input? Consider to use the Delegated events version of on function when creating elements on-the-fly. api.jquery.com/on Commented May 1, 2015 at 18:43
  • Where is your ok button and how are you handling the click to get the input? Commented May 1, 2015 at 18:45
  • I have edited my codes. can u say now? Commented May 1, 2015 at 18:52
  • where is option_number defined and set? Commented May 1, 2015 at 18:54

1 Answer 1

3

You should have use event delegation .on() function to access dynamically added content.. Here is the example to achieve that, hope this help:

<script>
 $(function(){
    $(document).on('click','#go2', function(){
        $('input.opt').each(function(){
              alert($(this).val());
         });

    }}

  });
  </script>
Sign up to request clarification or add additional context in comments.

2 Comments

i can get input from 2nd input filed. pls edit answer how to get from 1st input field using class text_area. its not working just changing the class name
yeah, i got answer. Thank u

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.