views:

5092

answers:

5

Need: Find a way to add a valid tag/attribute/property to a normal html control.

What I have is some javascript/jquery adding a click event to a link that will show or hide a div. The idea is to do this using $(document).ready and an anonymous method to create the method called by onClick at the page load. When clicked, a div will be shown with some text. This is all well and good except I can't figure out how to set up the text so that this can be done on the page load. What I'd like is something like:

<a href="..." class="showItLink" textToShow="This is the text to show">HI</a>

so that I can do this:

$(document).ready
(
  function()
  {
    $("..showItLink").click
    (
      function(event) 
      {
        var containerPosition;
        var createdDiv;

        //see if the div already exists
        createdDiv = $(this).children(".postComment");

        if (createdDiv.length == 0) 
        {
          //This is where the attribute is used so that the CreateDiv
          //method can take the textToShow and fill the div's innerText
          //with it                  V V V V V V
          createdDiv = CreateDiv(this.textToShow, "postComment"); 
          $(this).append(createdDiv);
          $(this).children(".postComment").hide();
        }

        $(createdDiv).toggle();
        event.preventDefault();
      }
    );
  }
);

Now besides not being xhtml valid (meh), this only works in IE. Firefox just says it doesn't exist. (this.textToShow) I could use something like rel or rev, but that seems just as hackish. I was wondering if there is a valid way of doing this.

Solution from answer below

comment = $(".showItLink").attr("comment");
...
createdDiv = CreateDiv(comment, "postComment");

Paired with:

<a href="http://www.theironical.com" class="showItLink" comment="hihihi" >HI</a>
A: 

The best way to do this kind of thing is to hide the text in another element and then toggle that element. Try something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
     <title>clear test</title>
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.js"&gt;&lt;/script&gt;
     <script type="text/javascript">
      $(document).ready(function() {
       $("#show-it").click(function() {
        $("#message").toggle();
       });
      });
     </script>
    </head>
    <body>
     <div>
      <a id="show-it" href="javascript:void(0);">show it</a>
      <div id="message" style="display:none;"> hidden message</div>
      hello world
     </div>
    </body>
</html>
Andrew Hare
+2  A: 

The way you add an attribute to an html control is by using the element.setAttribute("attributeName", "attributeValue") where "element" is the element you want to add the attribute to.

To get an attribute you use getAttribute("attributeName");

ferrari fan
+3  A: 

If you're using JQuery, just get and set the attributes with .attr().

Get: this.attr("textToShow")

Set: this.attr("textToShow", value)

Nerdling
This works and looks like the best way to do it. still means I have to break xhtml validity but not sure I'm going to cry yet...
Programmin Tool
I'd move away from XHTML, personally. I went back to HTML 4 until HTML 5 came out.
Nerdling
It's nothing to do with XML, a ‘textToShow’ attribute is exactly as invalid in legacy HTML as it is in XHTML.
bobince
Fair enough, this is a hack no matter how I try to look at it.
Programmin Tool
+1  A: 

You can't get away with adding custom attributes to HTML elements whilst still being valid. It will generally work in current browsers, but it's a bit fragile in that if you happen to pick a name that is in use (now or in the future) as an HTML or JavaScript property by any browser, the clash will stop it from working.

HTML5 proposes attributes whose names start with “data-​” as valid extension mechanisms. You could also consider namespaced elements in XHTML; this still isn't technically “valid XHTML” by the DTD but at least it is safe from collisions.

<a href="..." class="showItLink" textToShow="This is the text to show">HI

I suggest using the ‘title’ attribute for this particular purpose.

bobince
Do you mean something like class="showToLink" xmlns:comment="This is the text to show"? Problem with title is that it's still using something for something it isn't... not to mention it shows up on hover.
Programmin Tool
By ‘namespaced’ I mean something like <a ... myns:comment="...">, where myns is declared in the root element and you would have to extend the DTD to make it ‘valid anything’ (though still not ‘valid XHTML’ exactly).
bobince
Showing up on hover is exactly why I picked ‘title’ for that example – it's a reasonable behaviour for the link to do when you hover it, and puts the text where search engines can see it. Obviously ‘title’ wouldn't be a blanket solution for all types of value-passing-to-scripts.
bobince
A: 

If your textToShow attribute was an expando property, then this.textToShow would not return undefined, but since it is a custom attribute, you need to use jQuery's this.attr("textToShow") or the standard DOM this.getAttribute("textToShow").

nickyt