Results 1 to 3 of 3

Thread: onFocus evet put in a variable

  1. #1
    Join Date
    Jun 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default onFocus evet put in a variable

    onFocus event is working for the following code:

    Code:
    <input name="org" type="text" class="style4" id="org" maxlength="42" value="Test" onfocus="javascript:if(this.value=='Test')this.value='';" size="19">
    But it is not working for the following position:

    Code:
    var strHtml1 = "";				
    
    strHtml1 = "<input name='org' type='text' class='style4' id='org' maxlength='42' value='Org' onfocus='javascript\:if(this.value==\'Organisation\')this.value=\'\'\;'  size='19'>";
    Is is there anything wrong for

    Code:
    onfocus='javascript\:if(this.value==\'Organisation\')this.value=\'\'\;'
    Please help me

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You've got your nested quoting all messed up, use:

    Code:
    var strHtml1 = '';
    strHtml1 = '<input name="org" type="text" class="style4" id="org" maxlength="42" value="Org" onfocus="javascript\:if(this.value==\'Organisation\')this.value=\'\'\;"  size="19">';
    When nesting quotes, it is always easier to see what you are doing if you make the outermost delimiter also be the least used and the inner-most nested type of quote.

    Also, for that to work like your first example does, it needs to be:

    Code:
    var strHtml1 = '';
    strHtml1 = '<input name="org" type="text" class="style4" id="org" maxlength="42" value="Organisation" onfocus="javascript\:if(this.value==\'Organisation\')this.value=\'\'\;"  size="19">';
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Or better (by far):
    Code:
    var e1 = document.createElement("input");
    e1.name = e1.id = "org";
    e1.type = "text";
    e1.className = "style4";
    e1.maxLength = 42;
    e1.value = "Test";
    e1.onfocus = function() {
      if(this.value === this.defaultValue)
        this.value = '';
    };
    e1.size = 19;
    Or perhaps (neater in my opinion):
    Code:
    function buildElement(tag, attrs, children) {
      var e = document.createElement(tag);
      for(var x in attrs)
        if(attrs.hasOwnProperty(x))
          e[x] = attrs[x];
    
      if(children)
        for(var i = 0, n = children.length; i < n; ++i)
          e.appendChild(children);
    
      return e;
    }
    
    var e1 = buildElement("input", {
      name: "org",
      id: "org",
      type: "text",
      className: "style4",
      maxLength: 42,
      value: "Organisation",
      onfocus: function() {
        if(this.value === this.defaultValue)
          this.value = '';
      },
      onblur: function() {
        if(!this.value)
          this.value = this.defaultValue;
      },
      size: 19
    });
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •