Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 977905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:58:41+00:00 2026-05-16T03:58:41+00:00

Outline My simple application takes text from a text box and calls an asp.net

  • 0

Outline

My simple application takes text from a text box and calls an asp.net page method using AJAX. A list of users that partially match that text and their userID is returned. I wish to fill an existing DIV on the page with a DIV for each result, containing the users name. Each DIV then needs a click event to be bound to it that can call a javasacript method that can then have access to the key – the userID.

Code

function SearchButtonHandler(event) {
    PageMethods.SearchInstructors($("#txtInstructorName").val(), SearchCallback);
    return false;
}

The text to search is passed to the ajax call and the SearchCallback method receives a json array of objects containing an integer ‘UserID’ and a string ‘Fullname’.

This is the first way I did it, creating a ‘fake’ attribute on the DIV and having the click handler extract it:

function RowClicked() {
   alert($(this).attr("userid"));
}

function SearchCallback(data) {
    var collection = data.Result;

    if (collection.length == 0) { //tell the user there were none
        $("#resultlist").hide();
        $("#nonefound").show();
    } else {
        $("#nonefound").hide();
        $("#resultlist").empty();
        //method 1 below
        $.each(collection, function(index, item) {
            $("#resultlist").append($("<div></div>").html(item.FullName).attr("userid", item.UserID).click(RowClicked));
        });

        $("#resultlist").show();
    }
}

I don’t really like creating ‘fake’ attributes but came up with the following method:

        $("#resultlist").append($("<div></div>").html(item.FullName).click(function() {
            alert(item.UserID);
        }));

So I have two alternatives that both work fine.

Question 1:

Is this second one less efficient? It seems to me that it needs to create a function for each DIV tag. Or is there another way to do it?

Question 2 (leading on from 1):

How is ‘item’ in scope when the click event is triggered? It clearly works, I just can’t see how that can be in scope!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:58:42+00:00Added an answer on May 16, 2026 at 3:58 am

    You can use .data() and $.data() to store custom attributes, like this:

    .data("userid", item.UserID)
    

    Then fetch it like this:

    alert($(this).data("userid"));
    

    Or, a bit more efficient (not creating an extra jQuery object wrapper):

    alert($.data(this, "userid"));
    

    This stores the data in $.cache on the page, you already have the custom $.expando attribute on the element (to store the click handler that’s also there), so you’re not adding anything invalid, you’re just using the expando data cache you already created.

    Note: The $.expando variable above isn’t accessible until jQuery 1.4+ when it was made “public”


    Edit, moving tangential part of the explanation here:

    You can investigate the $.cache object like this from an element (don’t use this in code!, it’s if you’re curious/want to poke):

    $.cache[this[$.expando]]
    

    This is equivalent to $.data(this), $.expando is a generated property when the page loads, looking something like jQuery1281004831434 (jquery + timestamp on load) This is an attribute added to your element that’s a key, for example: jQuery1281004831434="4", in that case the cache object has a entry called 4 that stores your elements data and events…since this was already created for your click handler, you might as well use it, if avoiding another custom attribute is your goal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a method outline by gregor ( Create cronjob with Zend Framework )
I am attempting to do something simple (I thought) - securing my application using
I'm using Wakelock in my application to prevent the phone from sleeping when the
I want to use ASP.NET Application Services but configure it to use a different
To outline: I have a parser that grabs Cell references using the following regex
I have an text outline tree where i display a lot of information There
I have an application I am attempting to convert from a flex 3 air
Using PyClips, I'm trying to build rules in Clips that dynamically retrieve data from
I want to produce a simple enough application which uses a QTreeView widget to
I have an application in which most requests are submitted via AJAX, though some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.