logi
logi

Reputation: 1

Change variable every time a link clicked

links:

<ul id="topics">
<? while ($row = mysql_fetch_object($result)) { ?>
<li>- <a href="#" title="<?=$row->t_topic;?>"><?=$row->t_topic;?></a></li>
<? } mysql_free_result($result); ?>
</ul>

jQuery code:

$(function() {
    $("a").click(function(){
        var title = $("a").attr("title");
        $("#main").html(title);
    });
});

'title' is different on every link. When I clicked a link, it doesn't read var 'title'.

Upvotes: 0

Views: 253

Answers (1)

ndp
ndp

Reputation: 21996

The code needs to read the title from a specific a tag that was clicked upon. This simple change should do it:

$(function() {
    $("a").click(function(){
        var title = $(this).attr("title"); // Note "this" here
        $("#main").html(title);
    });
});

Upvotes: 3

Related Questions