Sample code for ListView onselectionchanged event RRS feed

  • Question

  • Hi,

    Please could someone post some sample code to get an onselectionchanged event to work in a JS / HTML 5 Windows Store app? I've tried loads of different examples on Google and none of them work. The control is on a seperate Page, and the .js 'code behind' for this page starts

    WinJS.UI.Pages.define("/../.html", {
        ready: function(element, options) {

    and I'm not really sure where I should be setting up the event handler, and where I should put the function that responds to the handler.


    Monday, December 17, 2012 11:36 AM


All replies

  • To add a bit more detail, here's what I have tried:

    1. In

    ready: function(element, options) {

    I have put this:

    var hello = document.getElementById("imageTextListCollection);
    hello.addEventListener("onselectionchanged", this.handler, false);

    where imageTextListCollection is the ID of the Div that is the ListView.

    Then after the ready function there are some more functions (unload, updateLayout) all seperated by commas. I have added another function this list called handler:

    handler: function(eventInfo) {
        var k = document.getElementById("imageTextListCollection");

    So I am expecting that when I click an item in the ListView (there are two in my current example) it will stick [Object] [object] into the eventlog in VS2012 - but nothing is happening.
    Monday, December 17, 2012 11:53 AM
  • Figured out what I was doing wrong (read the manual...):

    1. The event handler is oniteminvoked, not onselectionchanged.

    2. You have to drop 'on' from the oniteminvoked, so the event listener is:

    hello.addEventListener("iteminvoked", this.handler, false);

    Monday, December 17, 2012 12:07 PM
  • This sample shows how to use the listview including events (selectionChanged as well):

    HTML ListView  essentials sample


    Jeff Sanders (MSFT)

    Monday, December 17, 2012 1:08 PM