user3760741
user3760741

Reputation: 163

accessing a class element in different jQuery page

I am using jQuery mobile and I have a similar <li> class in different pages. Like this:

<div data-role="page" id="engineering">
    <h1 style="color:black;"> Departments </h1>

    <ul data-role="listview" data-theme="a" data-split-theme="b" data-split-icon="plus" data-inset="true">
    <li class="departments"><a href="#department">Department of Electrical and Electronics</a></li>
    <li class="departments"><a href="#department">Department of Computer Science</a></li>
    </ul>
</div>
<div data-role="page" id="medicine">
    <h1 style="color:black;"> Departments </h1>
    <ul data-role="listview" data-theme="a" data-split-theme="b" data-split-icon="plus" data-inset="true">
    <li class="departments"><a href="#department">Department of Medical Sciences</a></li>
    </ul>
</div>

Using $('departments').text(), this returns all of the text of the class departments. How can i get text of the specific list element that i click?

Upvotes: 0

Views: 31

Answers (2)

Pratik Joshi
Pratik Joshi

Reputation: 11693

use

inside click event for .department class:

$(this).text(); // this refers to current clicked element

to get text of Current element which got clicked.

If u had id , then u could use

$(this).attr("id").text();

This is better convention , if u have to deal with more complex operations with Clicked element.So most of the time Opt for ID to element.

Is corrent . It means Take text of element which Got clicked so THIS will point to CLICKED element ,say ".departments" .

Upvotes: 0

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62498

you have to get it this way:

$('.departments').click(function(){

$(this).text(); // this refers to current clicked element

})

FIDDLE DEMO

Upvotes: 3

Related Questions