tags:

views:

68

answers:

3

Dear all I have some class like this

<div class = "student">
<div class = "name">Adam </div>
<input class = "paperTaken">
</div>

<div class = "student">
<div class = "name">Smith</div>
<input class = "paperTaken">
</div>

When I put the cusor on an input field (say, "paperTaken" in the first "student" class), I can get the corresponding value in class "name" (eg: Adam).

Could you help me? Thank you in advance.

P/S:

Actually I used it for autoComplete with the Jquery Autocomplete Plugin , so I must listen to paperTaken for autocomplete

$(function(){ 
autoComplete(); 
} 

function autoComplete() { 
//This will result list of papers that StudentName take 
$(".paperTaken").autocomplete(baseUrl+"/autocomplete.php?student=StudentName 
... } 

I want to get the student name to put into the request for autocomplete. So I tried to apply your suggestion using

$('.paperTaken').focus(function() {
 ... 
} 

But it's seem that not work.

I'm wondering if the "papertaken" was already listened by autocomplete, can it listen to the "focus" call?

+2  A: 

I assume you want to fill the input with the content of the element with class name? Then following might work (untested):

$('.paperTaken').focus(function(){
  $(this).val($(this).siblings('.name').text());
});
azatoth
Thank you for your response. I tried the following:$('.paperTaken').focus(function(){ $("Test").html($(this).siblings('.name').text());});But don't return to any result.I've edited my question to clarify the purpose. Can you review and help me?Thank you.
Thang Nguyen
A: 
  1. List item

$(document).ready(function() {

    $('input.paperTaken').focus(function() {
        var value=$(this).siblings('.name').text();
        $(this).val(value);
    });

});

sushil bharwani
+2  A: 
$('.paperTaken').focus(function() {
       // $(this) references the <input> element that received focus
       // .prev() references the sibling that comes directly before $(this)
       // .text() will return the text value of the .prev() element
    var name = $(this).prev().text();

      // Log the result to the console, or use it however you need
    console.log( name );
});

Relevant jQuery docs:

patrick dw