SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Set event from JavaScript Code

    Hello,
    I have searched in google, but I didn't find the answer.
    I want to creat something like that using javascript:
    Code:
    <input type="text" onfocus="this.select();lcs(this);" onclick="event.cancelBubble=true;this.select();lcs(this);"  />
    here is my try, which works fine in Firefox but the events are not working in IE.
    Code:
     var parent  = document.getElementById(parentID);
          newInputFrom         = document.createElement('input');
          newInputFrom.type    = 'text';
          newInputFrom.setAttribute("onfocus","this.select();lcs(this);");
          newInputFrom.setAttribute("onclick","event.cancelBubble=true;this.select();lcs(this);");
    Any idea?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    You shouldn't use setAttribute to set events.
    Try using the following instead.

    Code javascript:
    newInputFrom.onfocus = function () { this.select(); lcs(this); };
    newInputFrom.onclick = function () { event.cancelBubble=true; this.select(); lcs(this); };

    Although, you should be able to update that last line to the following

    Code javascript:
    newInputFrom.onclick = function () { this.onfocus(); return false; };
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply. Now it's working in opera and IE , but in FF the calendar disappear in second.
    PS: this code is for a calendar, when you click the text field a calender pop up.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Now that we know more about the application it's being used for, it can be determined that the cancelBubble performs a greater task than originally apparent. That's an IE specific function though, so we'll have to use stopPropagation to achieve a similar task in all the other browsers.

    Since the onfocus event happens when the onclick event one occurs, it shouldn't be necessary to duplicate what's already done from the onfocus event.

    Code javascript:
    newInputFrom.onclick = function (e) {
        e = e || window.event;
        e.cancelBubble = true;
        if (e.stopPropagation) {
            e.stopPropagation;
        }
    }

    For more about events check out http://www.quirksmode.org/js/events_order.html
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •