wiki
wiki

Reputation: 3439

How do you get a value onClick from outside the anchor that is clicked?

Assuming the following HTML:

<tr id="record_1" >
<td id="name_1"><a href="#" id="logNameAndValue">Charles Dickens</a></td><td id="value_1">A Christmas Carol</td>
</tr>

How can I print out name=Charles Dickens value=A Christmas Carol by partially using the following jQuery:

$('#logNameAndValue').click( function(event){
    name=?
    value=?
    console.log("name=" + name + " value=" + value);
});

Upvotes: 0

Views: 604

Answers (4)

Pieter
Pieter

Reputation: 2199

$('#logNameAndValue').click( function(event){ 
    name=$(this).text();
    value=$(this).parent().next("td").text();
    console.log("name=" + name + " value=" + value);
}); 

Upvotes: 1

shoebox639
shoebox639

Reputation: 2312

name = $(this).text()
value = $('#value_' + $(this).attr('id').substring($(this).attr('id').length-1)).text()

Upvotes: 1

Brad Christie
Brad Christie

Reputation: 101614

In the click event function, the value would be $(this).siblings('td').text() I believe, and the name would be $(this).text().

Upvotes: 0

Justin Niessner
Justin Niessner

Reputation: 245429

$('#logNameAndValue').click(function(event){
    name = $(this).text();
    value = $(this).parent().next().text();
    console.log("name=" + name + " value=" + value);
});

Upvotes: 2

Related Questions