Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery show() after hide()

HTML:

<table id="table1">
    <tr>
        <th>1</th>
        <th>2</th>
        <th>3</th>
    </tr>
    <tr>
        <td class="table1column1"><input type="text" id="idInput1" /></td>
        <td class="table1column2"><input type="text" id="idInput2" /></td>
        <td class="table1column3"><input type="text" id="idInput3" /></td>
        </tr>
</table>
<button>Hide-Text-Show</button>

JQuery:

$(document).ready(function() {
    $('button').click(function() {
        $('#idInput1').hide();
        $('.table1column1').text('Test');
        $('#idInput1').show();
    });
});

http://jsfiddle.net/QNxyG/

I don't understand why when I add a text in td element, the show() method doesn't work?

Thanks

like image 550
user1889867 Avatar asked Feb 04 '26 06:02

user1889867


2 Answers

http://jsfiddle.net/QNxyG/4/

with .text() you override anything in your example... so the input doesnt exist anymore

HTML

<table id="table1">
    <tr>
        <th>1</th>
        <th>2</th>
        <th>3</th>
    </tr>
    <tr>
        <td class="table1column1"><span class="text" style="display:none;"></span><input type="text" id="idInput1" /></td>
        <td class="table1column2"><span class="text" style="display:none;"></span><input type="text" id="idInput2" /></td>
        <td class="table1column3"><span class="text" style="display:none;"></span><input type="text" id="idInput3" /></td>
    </tr>
</table>

<button>Hide-Text-Show</button>

jQuery

$(document).ready(function() {
    $('button').click(function() {
        var input = $('#idInput1');
        var text = input.parent('td').find('.text');
        text.text('text');
        text.toggle();
        input.toggle();
    });
});​
like image 193
Mik Avatar answered Feb 05 '26 22:02

Mik


Because with .text() you overwrite the #idInput1 element (it gets removed) so the next $('#idInput1') does not find an element to show..

like image 34
Gabriele Petrioli Avatar answered Feb 05 '26 21:02

Gabriele Petrioli