1
<div class="panel-footer">
    <div class="input-group">
        <input id="btn-input" type="text" class="form-control input-sm chat_input" placeholder="Wpisz tutaj swoją wiadomość..." />
        <span class="input-group-btn">
            <button class="btn btn-primary btn-sm" id="btn-chat">Wyślij</button>
        </span>
    </div>
</div>

I have HTML like above. My question is how to get the input value when I click on #btn-chat. I have been trying of jQuery functions like .prev() and .prevAll() but those didn't work for me.

5
  • 1
    Assuming #btn-chat is $(this), you can use $(this).parents(".input-group").find("#btn-input"). But that input has an id, so if it's unique, you can just call $("btn-input") directly. If it's not unique, I'd recommend using a class instead of an id. Commented Jan 6, 2016 at 23:49
  • 1
    You can use $('#btn-input').val() directly. Commented Jan 6, 2016 at 23:50
  • $(this).prev().prev().val() Commented Jan 7, 2016 at 5:59
  • @KiranBhagat prev() looks only at siblings, not parents. Commented Jan 7, 2016 at 7:52
  • $(this).parent().prev().val() Commented Jan 7, 2016 at 8:10

3 Answers 3

1

Given that the input element has an id attribute it should be unique, so you can select it directly without the need to traverse the DOM:

$('#btn-chat').click(function() {
    var inputVal = $('#btn-input').val();
    // do something with the value here...
});

If, for whatever reason, you still want to use DOM traversal you can use closest() to get the nearest common parent to both elements, and then find():

$('#btn-chat').click(function() {
    var inputVal = $(this).closest('.input-group').find('input').val();
    // do something with the value here...
});

If you have multiple elements in your page with the same id attribute then your HTML is invalid and you would need to change it. In that case, use class attributes to identify and select the elements.

Sign up to request clarification or add additional context in comments.

Comments

0
$("#btn-chat").click(function(){
    var input_values = $(this).parent().parent().find("input").val();
    alert(input_values);
});

Comments

0

As @RoryMcCrossan has pointed out, if you have multiple elements with the same id attribute, you would need to use a class attribute instead.

$(function() {
    $('.btn-chat').on('click', function() {
        var val = $(this).closest('.input-group').find('input.btn-input').val();
                  //OR $(this).parent().prev().val();
        console.log( val );
    });
});

$(function() {
    $('.btn-chat').on('click', function() {
        var val = $(this).closest('.input-group').find('input.btn-input').val();
        console.log( val );
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="panel-footer">
    <div class="input-group">
        <input type="text" class="form-control input-sm chat_input btn-input" placeholder="Wpisz tutaj swoją wiadomość..." />
        <span class="input-group-btn">
            <button class="btn btn-primary btn-sm btn-chat">Wyślij</button>
        </span>
    </div>
</div>


<div class="panel-footer">
    <div class="input-group">
        <input type="text" class="form-control input-sm chat_input btn-input" placeholder="Wpisz tutaj swoją wiadomość..." />
        <span class="input-group-btn">
            <button class="btn btn-primary btn-sm btn-chat">Wyślij</button>
        </span>
    </div>
</div>

<div class="panel-footer">
    <div class="input-group">
        <input type="text" class="form-control input-sm chat_input btn-input" placeholder="Wpisz tutaj swoją wiadomość..." />
        <span class="input-group-btn">
            <button class="btn btn-primary btn-sm btn-chat">Wyślij</button>
        </span>
    </div>
</div>

1 Comment

Great! Feel free to upvote the answer. And if it's what you were looking for feel free to accept it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.