javascript, selected by Text

1

I would like to select <selected=true> based on text and not value . I tried this way:

function setSelectBoxByText(eid, etxt) {
    var eid = document.getElementById(eid);
    for (var i = 0; i < eid.options.length; ++i) {
        if (eid.options[i].text === etxt)
            eid.options[i].selected = true;
    }
}

Basically I have a DropDownList that when I select it sends the parameter to an IFRAME and it executes a query that returns me the key word, with that keyword I want to leave selected another DropDownList Iframe returns:

<script type="text/javascript">parent.document.setSelectBoxByText('DDL_CategoriaMega','Casa');</script>

But it is not working.

Page Details Example: Page that calls the iframe

 <script type="text/javascript">
    function MudaDDLMega(valor) {
        document.getElementById('IFRAME_TESTE').src = "/busca_categoria_MEGA.aspx?IDCat=" + valor;
    }
 function setSelectBoxByText(eid, etxt) {
            alert('tste');
            var eid = document.getElementById(eid);
            for (var i = 0; i < eid.options.length; ++i) {
                if (eid.options[i].text === etxt)
                    eid.options[i].selected = true;
            }
        }

HTML

 <select id="DDL_Categoria" onchange="MudaDDLMega(this.value)">
    <option selected="selected" value="">Selecione</option>
    <option value="204">Academia de Escalada</option>
    <option value="228">Academias</option>
    <option value="1">Alugados</option>

Field you'd like to change based on this first

 <select id="DDL_CategoriaMega">
    <option value="1">Apartamento</option>
    <option value="2">Casa</option>
    <option value="3">Casa em Condom&#237;nio</option>
    <option value="4">Ch&#225;cara  Fazenda  S&#237;tio</option>
    <option value="5">Flat</option>
</select>
    
asked by anonymous 26.05.2014 / 17:21

1 answer

1

I discovered the answer, I do not know if I delete this post or post the answer:

The answer is: in the page that calls the iframe

function setSelectBoxByText(eid, etxt) {
            alert('tste');
            var eid = document.getElementById(eid);
            for (var i = 0; i < eid.options.length; ++i) {
                if (eid.options[i].text === etxt)
                    eid.options[i].selected = true;
            }
        }

and in the iframe I call:

<script type="text/javascript">parent.setSelectBoxByText('DDL_CategoriaMega','Casa');</script>
    
26.05.2014 / 18:12