Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am new to mvc and javascript and I am trying to develop a small question bank type website. I have a web page where the user can enter his question in a textbox and as answers for the question he can create as many options as he want (since it is a objective type question).

My view is as follows:

@model QuestionBank_Razor1.Models.HomeModel
@{ViewBag.Title = "Create your own questions";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{

@Html.TextBoxFor(m=>m.YourQuestion, new { placeholder="Your Question"})


@Html.ActionLink("Add Options", "AddOptions", new { onclick="add();"})
}
}

I want to generate a text box when the user clicks on "Add Options" link so that the user can enter his option in the text box. I want the text box to generate as many times as the link is clicked.

I think I will have to use javascript here, but I don't know how.

I have tried to use the below script, but it is throwing exception in form.appendChild()

XML
<script type="text/javascript" >
        function add() {
            var i = 0;
            var form = document.getElementById("form");
            var input = document.createElement("input");
            input.type = "text";
            input.id = "txtBoxOptions" + i + 1;
            form.appendChild(input);
        }
        </script>


Help!
Posted

1 solution

var form = document.getElementById("form");


View the source of your page, are there any elements with an id of form? If not then form is going to be null. You need to add the id yourself

@using (Html.BeginForm("YourAction", "YourController", FormMethod.Post, new { id = "form" }))


Or you can use js like

JavaScript
document.forms[0].appendChild(input);


Also you'll need to move your "i" variable outside the function otherwise every box will have the same id. Also you only need an id for css and javascript, if the input is submitted as part of a form you'll need to give them a name attribute also.

JavaScript
var i = 0;
function add() {
    var form = document.getElementById("form");
    var input = document.createElement("input");
    input.type = "text";
    input.id = "txtBoxOptions" + i + 1;
    input.name = input.id; // not sure if this is valid, I haven't tested it
    document.forms[0].appendChild(input);
}
 
Share this answer
 
Comments
Mary_Bhatta 13-Sep-15 8:41am    
I don't know what I am doing wrong, but on clicking the link which is supposed to generate the text boxes, now I am being taken to "Server Error in '/' Application." page with the message as "The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Home/AddOptions".

Thanks in Advance!
F-ES Sitecore 13-Sep-15 8:58am    
If you're using an onclick event on a button make sure it returns false;

<input onclick="add(); return false;" ...
Mary_Bhatta 13-Sep-15 10:20am    
Thanks so much!!! Solved my problem totally... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900