Prozak123
Prozak123

Reputation: 27

jquery, why id selector doesn't work

On the start page, i need hide a div dlaFirmy. this notation $('#dlaFirmy').hide(); does not work. If i use $('table').hide(); and it can easily hide.

Probably above statement is incorrect. Thank you in advance for your help

javascript code:
 <script>
    $(document).ready(function(){     
    $('#dlaFirmy').hide();
    $('select').on('change', function (e) {
       var optionSelected = $(this).find("option:selected");
       var valueSelected  = optionSelected.val();
       if( valueSelected === "2"){
           $('#dlaFirmy').show();
            alert("sdfsd");
       }
       else {
           $('#dlaFirmy').hide();
       }

       });
       });

Html code :

    <div class="formularzOC">
        <form id="form1" action="dodajDistrictManager.do" method="POST">
                <div id="podmianka"> <b>Dodaj nowy Lead: </b></div>

        <table>
            <select name="thelist">
                <option value="1">Klient Indywidualny</option>
                <option value="2">Klient Biznesowy</option>
            </select> 
            <tr>
                <td>Imię:</td><td><input type="text" name="imie" ></td>
            </tr>
            <tr>
                <td>Nazwisko:</td><td><input type="text" name="nazwisko" ></td>
            </tr>
            ...

           <div id="dlaFirmy"> 
                <tr>
                    <td>Nazwa firmy:</td><td><input type="text" name="email" ></td>
                </tr>
                <tr>
                    <td>Nip:</td><td><input type="text" name="email" ></td>
                </tr>
           </div>

        </table>
            <button type="submit"> Zatwierdź</button>
    </form>
        </div>

Upvotes: 0

Views: 83

Answers (2)

David Lounsbrough
David Lounsbrough

Reputation: 430

In one case you are hiding a table, and in the other you are hiding a div. This is why you get different results.

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1073968

Your markup is wildly invalid, you can't rely on anything that the browser actually does.

table elements cannot directly contain select elements or div elements. If you put them inside a table, the browser is likely to move them outside of it.

div elements cannot directly contain tr elements.

Bottom line: You have to make your markup valid. For what you're trying to do, tbody rather than div (for #dlaFirmy) might make sense.

Upvotes: 3

Related Questions