Blog Comments

  1. traq's Avatar
    I wonder if you could instead change the type of the input (i.e., from password to text) on the fly...?

    Edit: Answer:

    HTML Code:
    <input type="password" id="pass">
    <input type="button" onclick="textpass( 'pass' );" value="Show/Hide Password">
    
    <script>
    function textpass(elID){
        var el = document.getElementById( elID ) || false;
        if( !el ){ return; }
        if( el.getAttribute( 'type' ) == 'text' ){ el.setAttribute( 'type','password' ); }
        else{ el.setAttribute( 'type','text' ); }
    }
    </script>
    Tested only in Chrome. I suspect this is not very cross-browser-able.
    Updated 12-05-2012 at 10:19 PM by traq
  2. mburt's Avatar
    Or to avoid these issues:

    Code:
    var i = { d: function(x) { return document.getElementById(x); } }
    //i.d("whatever");
  3. jscheuer1's Avatar
    Quote Originally Posted by keyboard1333
    I specifically stated that it wasn't for people who use javascript libraries. Though I guess you do have a point. Mabye a symbol that is less used like the underscore (_) would be a better choice.
    And who never will use any script libraries that use the $ variable. It's good to plan ahead.

    Underscore might be a better choice, but eventually would run afoul of someone else who had a similar yet different idea for the underscore if your code were combined with theirs.

    The general rule with global variables is to keep them long and descriptive so as to avoid conflicts with other scripts and to make the code human readable.

    Shorter but still descriptive variables can be used within the local scope of a function.

    In the long run, any extra typing this involves will by far be made up for by the ease of reading and understanding of the code (years later or for someone looking at it for the first time), and its ability to 'play nice' with other code.

    Incidentally, jQuery can coexist with a script or script library that uses $ like you do or for any other purpose via its noConflict() mode, but that adds a layer of complexity. And as far as I know, none of the other $ hogging libraries can do that.
  4. keyboard's Avatar
    This is for all the people who don't use a javascript library like jQuery.
    I specifically stated that it wasn't for people who use javascript libraries. Though I guess you do have a point. Mabye a symbol that is less used like the underscore (_) would be a better choice.
  5. jscheuer1's Avatar
    It's unwise to pollute your code with a global variable which is so commonly used by other libraries. It makes all your code potentially (probably) incompatible with them. If I'm writing in ordinary javascript, I use a clip collection. One double click and I get:

    document.getElementById('');

    with the cursor insertion point between the two ' marks, all the time savings I need.