javascript, Selected by Text

Asked

Viewed 265 times

1

I would like to select <selected=true> on the basis of text and not in the value. I tried that 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 selecting sends the parameter to an IFRAME and this executes a query that returns me the keyword, with this keyword I want to leave another Dropdownlist The iframe returns:

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

But it’s 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 I’d like you to change based on that 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>
  • 1

    Looking over the function setSelectBoxByText seems to be right... I imagine it has something to do with the iframe itself. When you execute var eid = document.getElementById(eid);, the eid is filled correctly? Try to give a console.log (or a alert, I don’t know), we eid.options[i].text. The options are correct?

  • is put an Alert('test'); and it does not appear, apparently this call via IFRAME is the problem.

1 answer

1


I found the answer, I do not know if I delete that post or post the answer:

The answer is: on 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.