1

I am creating a user control that will create a series of html elements dynamically. Some of these controls will be database-driven cascading drop-downs. I've seen the JQuery .data() method suggested and used in examples for storing additional data about controls, but I don't know how to implement it on dynamically created elements.

I'm adding elements this way:

new LiteralControl(@"<select onchange=""DoSomething()""></select>")

So, how do I go about using .data() on these newly created elements so that the additional data is stored when they're created? I wanted to use custom attributes, but I gather that's not widely supported and can be problematic in legacy browsers.

I am very new to web stuff, so by all means if I didn't explain something well, let me know.

9
  • 1
    How about just adding data-foo="bar" attributes? Commented Jul 24, 2012 at 22:02
  • @zerkms I was under the impression that custom attributes aren't supported until html-5 and that some legacy browsers will complain. Commented Jul 24, 2012 at 22:03
  • Custom attributes work in pretty much all browsers in use these days, even including ten-year-old versions of IE, though it's best to use html5 data-xzy attributes. (Officially, custom attributes may be "invalid", but that doesn't mean they won't work.) Commented Jul 24, 2012 at 22:03
  • @Yatrix: I've never heard of any browser "complaining" about invalid attributes. In this respect, HTML5 is just trying to rein in what people were already doing. Commented Jul 24, 2012 at 22:04
  • Well, if the SO community says it's cool, it must be. I was using those anyway, but I thought it was going to be a problem. Thanks all! Commented Jul 24, 2012 at 22:04

1 Answer 1

1

If you know which data the elements should store by the time you are creating those elements, you could use data-* attributes.

new LiteralControl(@"<select data-id="1" onchange=""DoSomething()""></select>")
Sign up to request clarification or add additional context in comments.

3 Comments

Not the answer I was looking for, but you're the only one that responded and it does solve my problem. =)
That's definitely the way to go
What do you want use data() for if you are generating the data on the server-side?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.