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 4610840
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:11:15+00:00 2026-05-22T01:11:15+00:00

Let’s say I’m generating markup through server-side code. I’m generating a bunch of HTML

  • 0

Let’s say I’m generating markup through server-side code. I’m generating a bunch of HTML tags but I want to add custom client-side behavior.

With JavaScript (if I had a reference to the DOM node) I could have written:

var myDOMNode = ...
myDOMNode.myCustomAttribute = "Hi!";

Now the issue here is that I don’t want to qualify every element with an unique id just to initialize data. And it’s really strange to me, that there’s not an easier and unobtrusive way to attach client-side behavior.

If I’m remembing this correctly, this is valid IE stuff.

<div onload="this.myCustomAttribute='Hi!'"></div>

If I was able to do this, I should be able to access it’s “data context” though the identifier ‘myCustomAttribute’, which is really what I want.

  • 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-22T01:11:16+00:00Added an answer on May 22, 2026 at 1:11 am

    I do appricate the input but I’ve finally figured this out and it’s the way I go about initialization that has been the thorn in my side.

    What you never wan’t do is to pollute your global namespace with a bunch of short lived identifiers. Any time you put id="" on an element you’re doing exactly that (same thing for any top level function). By relying on jQuery, HTML5 data and CSS there’s a solution to my problem which I think is quite elegant.

    What I do is that I reserve a CSS class for a specific behavior and then use HTML5 data to parameterize the behavior. When the document is ready, I query the document (using Query) for the CSS class that represents the behavior and initialize the client-side behavior.

    I’ve been doing a lot of ASP.NET and within this context both the id="" and name="" belongs to ASP.NET and is pretty useless for anything else than internal ASP.NET stuff. What you typically find yourself doing is to get at a server-side property called ClientID you can refer to this from client-side JavaScript, it’s a lot of hassle. They made it easier in 4.0 but fundamentally I think it’s pretty much broken.

    Using this hybrid of CSS, HTML5 data and jQuery solves this problem altogether. Here’s an example of an attached behavior that uses regular expressions to validate the input of a textbox.

    <input type="text" class="-input-regex" data-regex="^[a-z]+$" />
    

    And here’s the script:

    $(function () {
    
        function checkRegex(inp) {
            if (inp.data("regex").test(inp.val()))
                inp.data("good-value", inp.val());
            else
                inp.val(inp.data("good-value"));
        }
    
        $(".-input-regex")
            .each(function () {
                // starting with jQuery 1.5 
                // you can get at HTML5 data like this
                var inp = $(this);
                var pattern = inp.data("regex");
                inp.data("regex", new RegExp(pattern));
                checkRegex(inp);
            })
            .keyup(function (e) {
                checkRegex($(this));
            })
            .change(function (e) {
                checkRegex($(this));
            })
            .bind("paste", undefined, function (e) {
                checkRegex($(this));
            })
            ;
    
    });
    

    Totally clean, no funky id="" or obtrusive dependency.

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

Sidebar

Related Questions

Let's say I have the following within my source code, and I want to
Let's say you create a wizard in an HTML form. One button goes back,
Let's say I have a drive such as C:\ , and I want to
Let's say i go to myblog.com/post/12 . The /post handler is already defined, but
let's say i want to build a smartphone app that tells a user when/where
Let's say I have a javascript array with a bunch of elements (anywhere from
Let us assume the code I use to iterate through a node list foreach(XmlNode
Let's say i have this block of code, <div id=id1> This is some text
Let's say I have the following models class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model):
Let's say the Activity I want to start is named OccupyThePieShop I was previously

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.