[Django]-Retrieve the input value by its name, which is within a div with an id

6👍

First you are missing a ' in $('#search),

Second you need to use .val() if you wish to get the value from the input.

$(document).ready( function(){
    $('#search').keyup(function() {
        $.ajax({
            type: "POST",
            url: "http://localhost:8000/overview",
            data: {
                'search_text': $('#search input[name=vehicle]').val()
            },
            success: searchSuccess,
            dataType: 'html'
        });
    });
});

Demo

$(document).ready( function(){
    $('#search').keyup(function() {
        console.log($('#search input[name=vehicle]').val())
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="search">
  <input type="text" name="vehicle">
</div>

0👍

Or you can use oninput event like this

 document.querySelector('input[name=vehicle]').oninput=function(){
    alert(this.value)
 };

OR

 $('input[name=vehicle]').oninput(function(){
 alert(this.value)
 });

Leave a comment