Reza San Reza San - 4 months ago 9
Javascript Question

jQuery : Get content of id

Need some help with jQuery DOM.
I need to extract the content of the id. I have a list that user can select and it will filter the

hotspot
of the map. After user select, I want
filtertitle
to display the new selected filter. I can apply the "id" to the filter title but not sure about the content because the content has Shopping / Dining. Someone please.

HTML

<div class="map-filter">
<div class="filtertitle">All Categories</div>
<ul class="filter-list">
<li class="active" id="All Categories">All Categories</li>
<li id="Shopping">Shopping/Dining</li>
<li id="Hotel">Hotels & Resorts</li>
<li id="Art">Art Galleries</li>
</ul>
</div>


jQuery

$('.filter-list li').click(function(){
var id = $(this).attr('id');
$(this).siblings().removeClass('active');
$(this).addClass('active');
$('.filtertitle').html(id); //This part is where I set the id
});

Answer

Use .text(), it gives you the text content of the desired element.

$('.filtertitle').html($(this).text());

In case of html content use .html().

$('.filtertitle').html($(this).html());
Comments