stackoverflow001
stackoverflow001

Reputation: 33

Unable to change text() value

<div class="search_on" id="search-item">
    <span class="text">one</span>
</div>
<ul class="search_item" style="display: none;">
    <li><a href="#">one</a></li>
    <li><a href="#">two</a></li>
    <li><a href="#">three</a></li>
</ul>

jQuery code:

$('.search_item li a').click(function() {        
    $('#search-item .text').text() = $(this).text();
    return false;
    $('ul.search_item').hide();
});

I want to get:when the user clicks the a link which in the li label, then change the value in <span class="text">one</span> to the value in the a label. Why does the above code not work?

Upvotes: 1

Views: 180

Answers (4)

Yograj Gupta
Yograj Gupta

Reputation: 9869

Change statement

$('#search-item .text').text() = $(this).text();

to

$('#search-item .text').text($(this).text());

and move return false statement, after $('ul.search_item').hide(); if you want to hide ul.search_item after setting the text.

Upvotes: 4

mram888
mram888

Reputation: 5139

Do this:

  $('#search-item .text').html( $(this).text());

Upvotes: 3

TheVillageIdiot
TheVillageIdiot

Reputation: 40512

change

$('#search-item .text').text() = $(this).text();

to

$('#search-item .text').text($(this).text());

with .text() = $(this).text() you are trying to set it like a property but it is a function so you need to call it like .text($(this).text())

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337743

You need to pass the text() value as a parameter. like this:

$('#search-item .text').text($(this).text());

Upvotes: 1

Related Questions