Killer Whale
Killer Whale

Reputation: 55

Get value element same level?

I have a code:

<table>
    <tr>
        <td>
            <input type="text" value="1" />
            <input id="button" type="button" value="Click me!" />
        </td>
    </tr>
</table>

I want to get value of input[type="text"] by below code but It doesn't work, please help me

<script type="text/javascript">
    $(document).ready(function() {
        $("#button").bind("click", function() {
            alert($(this).siblings().find('input[type="hidden"]').val(););
        });
    });
</script>

Upvotes: 0

Views: 257

Answers (8)

Ian
Ian

Reputation: 34489

Well the first problem is that you're trying to find hidden inputs, rather than text based inputs. You also need to move your filter into the siblings call to make it work correctly, although you did have an extra semi-colon too:

$(document).ready(function() {
    $("#button").bind("click", function() {
        alert($(this).siblings('input[type="text"]').val());
    });
});

See JSFiddle

Upvotes: 2

sudhansu63
sudhansu63

Reputation: 6180

Try this.

<script type="text/javascript">
    $(document).ready(function() {
        $("#button").bind("click", function() {
            alert($(this).parent().find('input[type=text]').val());
        });
    });
</script>

Upvotes: 1

Amit
Amit

Reputation: 15387

First of all, Your input type is text not the hidden. You are fetching data using hidden in Jquery then use as below if this is hidden:

$(document).ready(function() {
    $("#button").bind("click", function() {            
        alert($(this).siblings('input[type="hidden"]').val());
    });
});

if this is text:

 $(document).ready(function() {
    $("#button").bind("click", function() {            
        alert($(this).siblings('input[type="text"]').val());
    });
});

Demo

Upvotes: 1

S. S. Rawat
S. S. Rawat

Reputation: 6111

Try this.

$("#button").click(function() {
            alert($(this).prev('input[type="text"]').val());
        });

Demo Here

Upvotes: 2

Anton
Anton

Reputation: 32581

.find() searches for children, so you cant to use it here. Just use siblings()

$(this).siblings('input').val()

Upvotes: 3

Adil
Adil

Reputation: 148120

You need to find in siblings not in descendants of siblings.

  alert($(this).siblings('input[type="hidden"]').val()); 

Upvotes: 2

roullie
roullie

Reputation: 2820

change

alert($(this).siblings().find('input[type="hidden"]').val());

to

alert($(this).siblings().find('input[type="text"]').val());

Upvotes: 0

Moazzam Khan
Moazzam Khan

Reputation: 3170

use jQuery prev() as in alert($(this).prev().val());

It gets the immediately preceding sibling.

Upvotes: 1

Related Questions