user6824563
user6824563

Reputation: 815

Get input inside same <td>

How can I get previous input inside the same ?

I have the following table:

start-loop
    <td>...</td>
    <td>...</td>
    <td align=center>
            <input type="hidden" id="ContactPW" name="ContactPW" value="#UserPW#" />
            <input type="hidden" id="ContactEmail" name="ContactEmail" value="#UserEmail#" />
            <input type="hidden" id="ContacName" name="ContactName" value="#UserName#" />
            <input class="btn btn-primary" type="button" onclick="test();" value="Login" />
    </td>
end- loop

I would like to get in my test() function the values for "ContactPW", "ContactEmail", and "ContactName", but using $("#ContactPW").val() I just get the first item in the loop, not the row that I click the button.

Does anyone know how I can do it?

Thanks

Upvotes: 0

Views: 61

Answers (1)

i3lai3la
i3lai3la

Reputation: 980

in your test function, you can use jquery .parent().find() which means, your btn elem will look for parent and find the child with the name that you define. function as sample below,

html

<input class="btn btn-primary" type="button" onclick="test(this);" value="Login" />

javascript

function test(e){
   $(e).parent().find("[name='ContactPW']").val();
}

Upvotes: 2

Related Questions