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

  • SEARCH
  • Home
  • 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 853653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:47:43+00:00 2026-05-15T07:47:43+00:00

I have a pretty complex chat application going on, and there are multiple chat

  • 0

I have a pretty complex chat application going on, and there are multiple chat panes, chat entries, chat submits, etc. going on in the same window. At first I was going to do something like….

<input type="text" class="chattext" id="chattext-42">
<input type="text" class="chattext" id="chattext-93">
<input type="button" class="chatsubmit" id="chatsubmit-42">
<input type="button" class="chatsubmit" id="chatsubmit-93">

… etc. (of course this is vastly simplified, they’d be in separate divs, separate visibilities, etc) So, when they clicked on a .chatsubmit, it would then get the id of that and find the last two characters for the chat ID. This presents some problems, as it would require rewrites if IDs changed lengths, and seems just plain inelegant to me.

I then remembered the .data() facility in jQuery… I thought, maybe I could do it more like this:

<input type="text" class="chattext"> ... and add a .data("id", 42) to this one
<input type="button" class="chatsubmit"> ... and add a .data("id", 42)

So that when they click chatsubmit, it gets the ID, and then finds the chattext with that ID and processes it. But looking at the documentation, I don’t see an easy way to search by this. For example, let’s say the event target in this case is the chatsubmit with the data(‘id’) of 42…

var ID = $(event.target).data('id'); // Sets it to 42
var chattext = ...

And here I run into the trouble. How do I find which DOM element matches a class of chattext and a data(‘id’) of 42? Is there any easy method, or do I have to search every .chattext for the one with an id of 42? Or is there another easy way of doing this?

I did consider the possibility of the container div having the ID, which would make it, I think,? slightly easier to get. But if this works, it could be dealing with things in other container divs as well, making that not a long-term solution.

Edit: Literally seconds after posting this, I found this: http://james.padolsey.com/javascript/extending-jquerys-selector-capabilities/ which includes information on extending the selector to data. So I’ll try that out, and in the meantime, is this a completely foolhardy way of handling this?

  • 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-15T07:47:43+00:00Added an answer on May 15, 2026 at 7:47 am

    I don’t see anything wrong with using “id”, and it’s definitely going to be the fastest way to find things. If you’re careful with the pattern you use (say, always placing the numeric key after a “-” or “_” character) then you can just use a regex to split the key value:

      var key = $(this).attr('className').replace(/-(\d+)$/, "$1");
    

    or whatever.

    Of course, you can still use the “data” feature to also store the key, if fetching it from the “id” value seems ugly. It wouldn’t bother me, however.

    edit — if you wanted to “clean it up” even more, you could always write a couple of jQuery plugins to make it possible to get the key, and to return a selector to be used to find “partner” elements of a given one.

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

Sidebar

Ask A Question

Stats

  • Questions 411k
  • Answers 411k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Option 3. If the function of web service is not… May 15, 2026 at 7:48 am
  • Editorial Team
    Editorial Team added an answer Great question. Many people are surprised this works at all… May 15, 2026 at 7:48 am
  • Editorial Team
    Editorial Team added an answer Yes - I've had the up/down arrow problem, and I… May 15, 2026 at 7:48 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.