locked
How can one dynamically extract an id of a link?

    Question

  • Goal:
    When a link is clicked, I extract the ID and perform some action.

     

    Below is my code:
    ------------------
    ready: function (element, options) {
    var favoriteLink = element.querySelector("#favoriteLink");
                favoriteLink.addEventListener("click", this.reply_Click, false);
            },
    ----
    ----

    reply_Click: function(mouseEvent) {
                <perform some action>
            },
    -----------------
    The code above assumes the id="favoritelink". How do I re-write the code to make it work when I DO NOT KNOW the id?

    Thanks!

     

    Tuesday, January 22, 2013 11:14 PM

Answers

  • Figured it out. Final code below. Thanks again!

    WinJS.Utilities.query("a", element.querySelector("#foo")).listen("click", function (eventInfo) {
       eventInfo.preventDefault();
       var link = eventInfo.currentTarget;
       var linkid = link.id;
    //use the id

                });

    • Marked as answer by Jay_Hawk Thursday, January 24, 2013 12:38 AM
    Thursday, January 24, 2013 12:38 AM
  • If you don't know the id you have to either select all the links or select the links contained in some container, you can use WinJS.Utilities.query:

    WinJS.Utilities.query("a").listen("click", function (eventInfo) {
       eventInfo.preventDefault();
       var link = eventInfo.currentTarget;
       var linkid = link.id;
    //use the id
    
                });


    • Marked as answer by Jay_Hawk Thursday, January 24, 2013 12:38 AM
    Wednesday, January 23, 2013 1:04 PM

All replies

  • If you don't know the id you have to either select all the links or select the links contained in some container, you can use WinJS.Utilities.query:

    WinJS.Utilities.query("a").listen("click", function (eventInfo) {
       eventInfo.preventDefault();
       var link = eventInfo.currentTarget;
       var linkid = link.id;
    //use the id
    
                });


    • Marked as answer by Jay_Hawk Thursday, January 24, 2013 12:38 AM
    Wednesday, January 23, 2013 1:04 PM
  • Thank You!

    If I wish to query for only those links which are contained in container "foo", what would be the syntax?

    Wednesday, January 23, 2013 11:10 PM
  • Figured it out. Final code below. Thanks again!

    WinJS.Utilities.query("a", element.querySelector("#foo")).listen("click", function (eventInfo) {
       eventInfo.preventDefault();
       var link = eventInfo.currentTarget;
       var linkid = link.id;
    //use the id

                });

    • Marked as answer by Jay_Hawk Thursday, January 24, 2013 12:38 AM
    Thursday, January 24, 2013 12:38 AM