Souad
Souad

Reputation: 5084

Auto select a value in Combobox based on a text

I have a table and when I click on a row i want values of this row printed on a form where i have a combobox. So the problem that i can just change the text of the option in the combo not select automatically an option ?

this is my script but it doesn't give me what i want :

var fvar=document.forms.f.fonction;

    for(var i=0;i<fvar.options.length;i++){
        if (document.getElementById('listuser')
        .getElementsByTagName('tr')[row.rowIndex].cells[2].textContent==fvar.options[i].text)
            {
            fvar.options[i].selected=true;
            }
    }

The HTML :

<table id="listuser">

    <thead>
        <tr>
            <th>Action</th>
            <th>Code</th>
            <th>Fonction</th>

        </tr>


        <tr id="lign" onClick="selectRowService(this)">
            <td><input type="checkbox" name="box"></td>
            <td><c:out value="${activity.cd_activite}" /></td>
            <td><c:out value="${activity.fonction}" /></td>
    </thead>


    <!-- Table body -->

    <tbody>
    </tbody>

</table>

<table id="tabmenu">

    <tr>
        <td>Fonction :</td>
        <td><div class="styled-select">
                <form:select name="fonction" path="fonction">

                    <c:forEach items="${fonc}" var="f">
                        <form:option value="${f.code_fonction}">${f.ll_fonc}</form:option>
                    </c:forEach>

                </form:select>
            </div></td>
    </tr>

</table>

Please is there any other way to select the option value automatically ?

Upvotes: 1

Views: 728

Answers (1)

K D
K D

Reputation: 5989

Try this

var fvar=document.forms.f.fonction;

    for(var i=0;i<fvar.options.length;i++){
        if (document.getElementById('listuser')
        .getElementsByTagName('tr')[row.rowIndex].cells[2].textContent==fvar.options[i].text)
            {
                 fvar.value = fvar.options[i].value;
            }
    }

Upvotes: 1

Related Questions