• This is Line...">
    Geek asks and answers

    Jquery simple example change () function

    I'm just learning jQuery, I'm so noob.

    see, we have 2 divs:

    <div id="div1">
    <ul>
        <li> This is Line 1</li>
        <li> This is Line 2</li>
        <li> This is Line 3</li>
        <li> This is Line 4</li>
        <li> This is Line 5</li>
    </ul>
    </div>
    
    <div id="div2">
    <ul>
        <li> <input type="text" /></li>
        <li> <input type="text" /></li>
        <li> <input type="text" /></li>
        <li> <input type="text" /></li>
        <li> <input type="text" /></li>
    </ul>
    </div>
    

    since I'm just learning the change () function, this is what I want to do: When each value of the input fields in div2 changes, div1 li of the same number should be changed to the entered value, for example, if we start typing in the second input field in div2 , the second text li should go, what we entered in the second input block.

    here is what i have tried so far but not working:

    <script type="text/javascript">
        $(document).ready(function(){
            $('#div2 :input').change(function(i){
                var str = $(this).text;
                $('#div1 li').eq(i).text(str);
            });
        });
    </script>
    

    What happened to this? thanks in advance

    +3
    jquery
    behz4d May 11 '12 at 18:15
    source share
    4 answers

    The parameter passed to the function change()is not an index. This is eventan object . You need to get the index yourself using index().

    , text() , val(), .

    $('#div2 :input').change(function() {
        var str = $(this).val(), // You need to use `val` to get the value (also, note the "()" here)
            index = $(this).parent().index(); // this gets the index of the input
        $('#div1 li').eq(index).text(str);
    });​
    

    DEMO: http://jsfiddle.net/skram/UJBCr/1/

    +6
    Rocket Hazmat 11 '12 18:25

    val() ( ) text():

     var str = $(this).val();
    
    +2
    Sarfraz 11 '12 18:17

    Change var str = $(this).text

    as

    var str = this.value; //or $(this).val()

    +1
    Selvakumar arumugam May 11 '12 at 18:16
    source share

    Use this:

     $('#div2 :input').change(function(e){
          var i = $('#div2 :input').index(this);
          $('#div1 li').eq(i).text( $(this).val() );
     });
    

    Demo

    +1
    Engineer May 11 '12 at 18:28
    source share

    More articles:

    • Вложенная вставка документа в MongoDB с С# - mongodb
    • How to determine the last non-zero decimal digit in a float? - c ++
    • Looping through an array - ruby ​​| fooobar.com
    • Do containers of primitive types in C # use value semantics or pointer / reference semantics? - c #
    • Using java code in pnml to represent Colored Petri Net - java
    • You can debug swc in flash device - flex
    • Writing PDF text to PDFContext on iOS - ios
    • Исходное изображение CSS не загружается в IE9 - css
    • Banner does not appear in IE7 and IE8 - internet-explorer
    • How to run opencv on a microcontroller? - c ++

    All Articles

    Geek-Ask | 2020