Live issue with jQuery

When I click an item from a list, jQuery gets the class from the clicked element and writes it to the input field. Then the value from the input should be written to div # without any action.

HTML:

<ul>
    <li class="NewYork">New York</li>
    <li class="Paris">Paris</li>
    <li class="Moscow">Moscow</li>
</ul>

<input type="text" id="city" value="" />

<div id="content"></div>

JQuery

$(document).ready(function() {
    $('ul li').live('click', function() {
        var select_value = $(this).attr('class');
        $('input#city').val(select_value);
        return false;
    });

    $('input#city').live('change', function() {
        var content = $(this).val();
        $('#content').text(content+' is beautiful city');
    });
});
+3
source share
1 answer

Change this:

$('input#city').val(select_value);

For this:

$('input#city').val(select_value).change();

An event is changefired when a value changes, usually in a text field blur(by clicking elsewhere). However, you can activate it if necessary with .change()or .trigger('change'), which makes the code above.

+3
source

Source: https://habr.com/ru/post/1739702/


All Articles