1

Using jQuery and backbone.js, I would like to separate the JavaScript from the HTML as much as possible.
Is it possible to add a custom attribute for a better separation between JavaScript and HTML? And then how can I use the new jQuery selector?

Example:

<input type="text" id="todo-input" class="todo-input" tid="tid-attribute" > hello world </input>

// The DOM events
// I would like to use another selector tid and not class or id
events: {
  "keypress #todo-input" : "updateOnEnter" // I would like to use another selector tid not id,
  "keypress .todo-input" : "updateOnEnter" // I would like to use another selector tid and not class
}
2
  • I still don't get it. Do you want separation ? or the attributes bother you. As far as i can tell this is work of the view, do not render anything from the server for the backbone app to depend on. This will make your backbone app deploy anywhere. Commented May 7, 2012 at 5:35
  • HTML class attributes are used for all sorts of things, not just for styling with CSS. There's nothing wrong with adding extra classes to an element just for use in selectors. Commented May 7, 2012 at 5:49

2 Answers 2

3

HTML5 allows for custom data attributes that start with data-:

<input type="text" data-custom-attr="somevalue">

Then in your backbone events declaration:

"keypress [data-custom-attr='somevalue']" : "updateOnEnter"
Sign up to request clarification or add additional context in comments.

Comments

0

you can use:

$('input[type="text"]').attr('tid', 'tid-attribute');

OR

$('input[type="text"]').attr('data-tid', 'tid-attribute');

Comments

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.