I have the following code in jQuery Mobile :
<ul data-role="listview" data-theme="d" id="#listview" data-divider-theme="d">
<li data-role="list-divider" class="name">Mr. Sam</li>
<li data-icon="false" class="details">
<a onclick="getInfo()">
And on my onclick, I want to access the Mr. Sam. Here’s the following code I’m trying in the JS:
var info = $(this).parent().parent().text();
Because, I’m clicking on <a>, it’s parent is <li>, and it’s parent is <li> containing Mr. Sam. But I’m doing something wrong, as alert(info); alerts a blank output.
What do I need to change? I’ve even tried, .html().
In your markup the target
liis not grand-parent of the clicked element, it’s previous sibling of the parent element:You can use
prevmethod:Also note that in your code
thisrefers to window object, you can passthisto your function:http://jsfiddle.net/F8wt8/
Since you are using jQuery (after removing
#fromuls ID attribute) instead ofonclickattribute you can useclickmethod.