Note that there are some explanatory texts on larger screens.

plurals
  1. POget clicked value from listview
    primarykey
    data
    text
    <p>I have created a dynamical listview with jQuery Mobile and I want to get the value of the clicked list item. The reason I want to achieve this is because I need this value to link to another page and show more details about the clicked item.</p> <p>I already have searched around the internet and I found a solution which doesn't work for me.</p> <p>My (jQuery Mobile) page looks like this:</p> <pre><code>&lt;div data-role="page" id="personList"&gt; &lt;div data-role="header" data-position="fixed"&gt; &lt;h1&gt;header&lt;/h1&gt; &lt;/div&gt; &lt;div data-role="content"&gt; &lt;/div&gt; &lt;div data-role="footer" data-position="fixed"&gt; footer &lt;/div&gt; &lt;/div&gt; </code></pre> <p>My function to dynamically add items to the listview looks like this:</p> <p>(I used HTML5 SQL web storage to store data local on a device)</p> <pre><code>function showPersons() { db.transaction (function (transaction) { //get data from table var sql = "SELECT * FROM person"; transaction.executeSql (sql, undefined, //loop through sql result and add results to html variable function (transaction, result) { var html = "&lt;ul id=" + "personListview" + "data-role=" + "listview" +" data-filter=" + "true" + "&gt;"; if (result.rows.length) //if there is something found { for (var i = 0; i &lt; result.rows.length; i++) { var row = result.rows.item (i); var voorNaam = row.voorNaam; var achterNaam = row.achterNaam; var email = row.email; html += "&lt;li data-theme='e'" + "data-name='test value'&gt;" + "&lt;a href='#'&gt;" + "&lt;h3&gt;" + voorNaam + " " + achterNaam + "&lt;/h3&gt;&lt;p&gt;" + email + "&lt;/p&gt;&lt;/a&gt;&lt;/li&gt;"; } } else //if there is nothing found { html += "&lt;li&gt; No persons found &lt;/li&gt;"; } html += "&lt;/ul&gt;"; //Change page $("#personList").unbind ().bind ("pagebeforeshow", function () { var $content = $("#personList div:jqmData(role=content)"); $content.html (html); var $ul = $content.find ("ul"); $ul.listview (); }); $.mobile.changePage ($("#personList")); }, error);});} </code></pre> <p>As you can see, I added "data-name='test value'" to the "li" tags so I should get this value with the following function:</p> <pre><code>$('#personListview').children('li').click(function() { alert($(this).attr('data-name')); }); </code></pre> <p>However, this last function doesn't work. Nothing happens when I click at an "li" item. I would expect I get an alert with the value of "data-name").</p> <p>PS. even when I have declared this, I doesn't get an alert:</p> <pre><code>$('#personListview').children('li').click(function() { alert('test'); </code></pre> <p>Could someone help me? I am a beginner with Javascript and jQuery(mobile).</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload