Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery get previous input text

I have the following html:

    <div class="active_string">
                <input type="text" class="answer" /><div class="images"><img src="/images/myImage.png" alt="" /></div>
                <a href="" class="check_quest" id="1">Click me</a>
</div>
    <div class="active_string">
                <input type="text" class="answer" /><div class="images"><img src="/images/myImage.png" alt="" /></div>
                <a href="" class="check_quest" id="2">Click me</a>
</div>

For every click on check_quest I want to get value of a corresponding input. How can I do it using query. I`ve tried the following:

$(this).prev().prev().text()

but this one and other combinations of prev text() and val are not showing the text Ive put into that textfield.

like image 227
Yaroslav Yakovlev Avatar asked Dec 10 '10 09:12

Yaroslav Yakovlev


People also ask

How to find input type text in jQuery?

$('. sys'). find('input[type=text],select').

How to check input type value in jQuery?

Answer: Use the jQuery val() Method You can simply use the jQuery val() method to get the value in an input text box. Try out the following example by entering something in the text input box and then click the "Show Value" button, it will display the result in an alert dialog box.

How to get element before element in jQuery?

prev( [selector ] )Returns: jQuery. Description: Get the immediately preceding sibling of each element in the set of matched elements. If a selector is provided, it retrieves the previous sibling only if it matches that selector.


2 Answers

I think

$(this).parent().find('input').val();

would be the best way to go.

like image 123
Haralan Dobrev Avatar answered Oct 13 '22 07:10

Haralan Dobrev


Have you tried $(this).prev('input')?

EDIT:

I mean: $(this).prevAll('input').val().

like image 22
Spiny Norman Avatar answered Oct 13 '22 07:10

Spiny Norman