SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,005
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    passing a value from one event listener to another...

    The html part:
    Code HTML4Strict:
    <div id='data'></div>
     
            <form action="">
                <input id="nomeInput" type="text" name="nomeInput" value="" autocomplete="false"/>
            </form>

    The js part:
    Code JavaScript:
    $(document).ready(function(){
       //add an event handler, on blur, execute all this function:
     
       $('#nomeInput').keypress(function(){
       $.post("testeBasico_1.php", {nomeInput : $('#nomeInput').val()}, function(resposta) {
    for (var x = 0, tamanhoDados = resposta.dados.length; x < tamanhoDados; x++){
    $('#data').append(resposta.dados[x]+'<br>');
    }
    $('#data').append('<br />'+resposta.venhoDoInput);
    }, "json");
    });//end of keypress;
     
     
    $('#nomeInput').blur(function(){
        alert('here, I would like to grab the value we see on the input field at this precise moment.');
                });
    	});

    The server side (php) part:
    PHP Code:
    $response = (object) array(
        
    'success' => TRUE,
        
    'dados'       => array("venhoDoServerSide1""venhoDoServerSide2""venhoDoServerSide3"),
        
    'venhoDoInput' => $_POST['nomeInput']
    );
    echo 
    json_encode($response); 

    This is the entry point for doing an autocomplete, but for know, I would just like to ask:

    What should we do so that, the text that is typed, appears on that alert box?


    Thanks in advance,
    Márcio

  2. #2
    SitePoint Zealot
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    alert($(this).val())

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,005
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot.

    So, "this" means "this element" ?
    (I'm sure it isn't that simple...)

    When we use the code suggested,
    Code JavaScript:
    $('#nomeInput').blur(function(){
    alert($(this).val())
    });

    We are telling, alert with the #nomeInput value ;

    So, actually we are not passing anything. We are just reading the element value again yes?

    When we see
    Code JavaScript:
    blur(function(){
    alert($(this).val())
    });

    is the function inside blur an event listener?

    Thanks a lot,
    Márcio

  4. #4
    SitePoint Zealot
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $(this) is a reference to the object firing the onblur, #nomeInput.

    Inside blur is an anonymous function. blur is the event listener.

    It's equivalent to the JavaScript:
    Code:
    document.getElementById('nomeInput').onblur = function() {alert(this.value;)};

  5. #5
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,005
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok...

    Quote Originally Posted by dogFang View Post
    $(this) is a reference to the object firing the onblur, #nomeInput.
    If $(this) is a reference of an object firing some event, am wondering: can we still have $(this) when no event is in place?

    Quote Originally Posted by dogFang View Post
    Inside blur is an anonymous function. blur is the event listener.
    So, since blur is an event listener. What does it listen? "the move out from an element". Is that anonymous function, a event handler? Like something that will do something after the even as been listen?

  6. #6
    SitePoint Zealot
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  7. #7
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,005
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the additional info.
    I will follow the path from here. :-)


    K. Regards,
    M&#225;rcio


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •