0

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?

2 Answers 2

3

you have to get it this way:

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

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

})

FIDDLE DEMO

Sign up to request clarification or add additional context in comments.

Comments

0

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" .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.