you are here: codestackercodes [RSS]rails inspired helpers in javascript

rails inspired helpers in javascript

var link_to = function (text, url, options) {
  var link = document.createElement('a');
  link.textContent = text;
  link.setAttribute('href', url);

  for (option in options) {
    link.setAttribute(option, options[option]);
  }

  return link;
};

var image_tag = function (alt, src, options) {
  var image = document.createElement('img');
  image.setAttribute('alt', alt);
  image.setAttribute('src', src);

  for (option in options) {
    image.setAttribute(option, options[option]);
  }

  return image;
};

link_to("Google", "http://google.com", { id: 321, title: "Google" });

image_tag("Google", "http://www.google.com/images/logos/ps_logo2.png", { id: "logo", title: "Google's Logo" });

Comments