How to load selected parameters value dynamic in javascript

I am using java + struts.

I am creating a dynamic row table and dynamically loading the values ​​of the select parameters using the javascript of my code.

function addRow() {

        var table = document.getElementById('tableID');
        var rowCount = table.rows.length;
        var countval = rowCount;
        var row = table.insertRow(rowCount);

        //country

        var cell1 = row.insertCell(0);
        var element1 = document.createElement("select");
        element1.style.width = "250px";
        element1.id = "countryId" + countval;

        var o = document.createElement('option');
        o.value = "0";
        o.text = "[SELECT]";
        element1.options.add(o);
        cell1.appendChild(element1);

        var o = document.createElement('option');
        o.value = "1";
        o.text = "India";
        element1.options.add(o);
        cell1.appendChild(element1);

}

      

this is the html code:

<input type="button" value="add" onclick="addRow()">
<table id="tableID"></table>

      

the value of the boot select option works on the struts tag, which works:

<html:select property="countryId">
   <html:optionsCollections name="countryForm" property="countryList" value="countryId" label="countryName"/>
</html:select>

      

above this code works, but i need the same logic using select option value using javascript. how can i load country values. pls any help me, thanks and go ahead

+3


source to share


1 answer


I don't understand why you need to create a dynamic selection even if resolved using the Struts tag.
But, if you need it, I can offer you a non-recommended way to embed location logic in Javascript.
similar;



function addRow(){
    alert("inside addRow");
    var list = new Array();
    var value = new Array();
    var i = 0;
    <logic:iterate name="countryForm" property="countryList" id="country">
      list[i] = '<bean:write name="country" property="countryName" />';
      value[i] = '<bean:write name="country" property="countryId" />';
      i++;
    </logic:iterate>

    var table = document.getElementById('tableID');
    var rowCount = table.rows.length;
    var countval = rowCount;
    var row = table.insertRow(rowCount);

    //country

    var cell1 = row.insertCell(0);
    var element1 = document.createElement("select");
    element1.style.width = "250px";
    element1.id = "countryId" + countval;

    var o = document.createElement('option');
    o.value = "0";
    o.text = "[SELECT]";
    element1.options.add(o);
    cell1.appendChild(element1);

    for(i=0; i<list.length; i++){
        var item = document.createElement('option');
        item.value = value[i];
        item.text = list[i];
        element1.options.add(item);
        cell1.appendChild(element1);
    }
}

      

+2


source







All Articles