Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for all types of elements (not just inputs) #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions jquery.datalink.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,51 @@ var oldcleandata = $.cleanData,
html: "html",
text: "text"
};

function getField(target) {
switch (target.nodeName.toLowerCase()) {
case "input":
case "select":
case "textarea":
return fnSetters.val;

case "h1":
case "h2":
case "h3":
case "h4":
case "h5":
case "h6":
case "h7":
case "li":
case "p":
case "blockquote":
case "address":
case "span":
case "div":
case "pre":
return fnSetters.text;

// links actually have multiple value fields
// like text and the link location
// it might be a good idea to be able to bind to specific propertys
// rather than just one binding per element
// also for getting/setting the href the jquery 'attr' method would have to be used,
// which would require additionl changes
case "a":
return fnSetters.text;

default:
return fnSetters.html;
}
}

function setValue(target, field, value) {
if ( target.nodeType ) {
// if this point was reached the supplied field would always be 'val'
// which only works with input elements
// therefore we have to figure out which setter method to use
field = getField(target);

var setter = fnSetters[ field ] || "attr";
$(target)[setter](value);
} else {
Expand Down