Frage

[jQuery] How do I get value of textbox relative to button?

The event is triggered on click of .button-action

$(this).closest(".dmsInput").find('.d').val();

The structure goes like this:

<td class="dmsInput">
 <input type="text" maxlength="4" size="4" class="d">
</td>
<td>
 <button class="button-action add">+</button>
</td>

I tried to alert the value but it returns undefined. Am I doing it right?

War es hilfreich?

Lösung

It is because .closest() will search the ancestor tree, but the dmsInput element is not an ancestor of the button element.

$(this).parent().prev(".dmsInput").find('.d').val();

or

$(this).closest('tr').find('.dmsInput .d').val();
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top