Adding new form field using JS

Hello

I’m using a CRUD generated _form.php in the Yii framework and want to allow the user to add additional dropdown list to the form at the click of a link.Below is my basic html and javascript. Im having proble trying to incorporate it into yii.

<html>

<head>

<script>

var id = 2;

function add_route_field()

{

var obj = document.getElementById("route");

var data = obj.innerHTML;

data += " <br/><select name=‘route[]’ id=‘route"+id+"’><option value=‘joburg’>joburg</option><option value=‘cpt’>cpt</option><option value=‘durban’>durban</option></select><br /><br />";

obj.innerHTML = data;

id++;

}

</script>

</head>

<body>

<form method="post" action="" enctype="multipart/form-data">

    &lt;div id=&quot;route&quot; align=&quot;right&quot;&gt;


      &lt;select name=&quot;route[]&quot; id=&quot;route&quot; /&gt;


      	&lt;option value=&quot;joburg&quot;&gt;joburg&lt;/option&gt;


        &lt;option value=&quot;cpt&quot;&gt;cpt&lt;/option&gt;


        &lt;option value=&quot;durban&quot;&gt;durban&lt;/option&gt;


      &lt;/select&gt;&lt;br /&gt;





    &lt;/div&gt;&lt;span onClick=&quot;add_route_field()&quot;&gt;add routing&lt;/span&gt;

</form>

</body>

</html>

Seems like you may want to use jquery plugin for field cloning (jquery.dynamic-form.js or something alike)

Try jqRelcopy extension and my ‘hack’ to make it work nicer with models.