Results 1 to 9 of 9

Thread: onChange Event not happening ..(:

  1. #1
    Join Date
    Nov 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy onChange Event not happening ..(:

    I am creating a SELECT box dynamically using javascript.

    dropBox = document.createElement("select");
    dropBox.id = "drpBox";
    dropBox.name = "drpBox";

    I want to add ONCHANGE event to "dropBox".

    I tried this, "dropBox.setAttribute ('onchange', 'someFunction(this)');"

    It is not working ...(:

    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

    That looks OK but, I would have done this:

    Code:
    dropBox.onchange=function(){
    any valid code here
    }
    or:

    Code:
    var someFunction=function(){
    alert('Hi');
    }
    
    dropBox.onchange=someFunction;
    The setAttribute() method might work for this but, the syntax may be different than you expect. If I were to try using it in this regard, I would try:

    dropBox.setAttribute ('onchange', 'alert("Hi")', 0)

    But, that works in FF, not IE (just tested it). The setAttribute() method isn't always well supported for all attributes in IE.
    - John
    ________________________

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

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by eric_2005
    I tried this, "dropBox.setAttribute ('onchange', 'someFunction(this)');"
    Don't use the setAttribute method to add intrinsic event listeners. Use event properties, which expect a function reference:

    Code:
    dropBox.onchange = function() {
      someFunction(this);
    };
    Quote Originally Posted by jscheuer1
    dropBox.setAttribute ('onchange', 'alert("Hi")', 0)
    The setAttribute method only has two arguments. Microsoft have added a third because of some odd case-sensitivity issues on their part, though that can be easily avoided.

    The setAttribute() method isn't always well supported for all attributes in IE.
    Indeed. I would recommend avoiding the method when scripting HTML documents (including XHTML served as HTML).

    Mike

  4. #4
    Join Date
    Nov 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    thanks mwinter & jscheuer1 , it worked...

    But now, I got another issue... below code is inside another loop, which got 3 records and for each record i have to get all the Emp id and name. But say in this example,since we have 3 records, and below recordset contains only one record, rest of the array is showing me "UNDEFINED". Please help me...

    //creates an array
    var a1 = new Array();
    a1[0]= new Array();
    a1[1]= new Array();

    <cfset counter = 0>

    <cfloop query="query1">
    a1[0][#counter#]= #empID#;
    a1[1][#counter#]= #empname#;
    <cfset counter = counter + 1>
    </cfloop>

  5. #5
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by eric_2005
    <cfset counter = 0>
    Sorry, never used ColdFusion.

    Mike

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

    Talking found solution

    hey i found a solution for the same issue

    objectname.setAttribute('onchange','someFunction()') didnt worked for me either.

    but you can do it in on of these ways

    method 1
    objectname.onchange = function someFunction() {
    //your code here
    }


    method 2
    objectname.onchange = someFunction

    //you define the function seprately here
    function someFunction() {
    //your code here
    }


    hope this will help

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

    Default

    Quote Originally Posted by velanzia
    method 2
    objectname.onchange = someFunction

    //you define the function seprately here
    function someFunction() {
    //your code here
    }
    Actually, you'd have to define the function first.
    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!

  8. #8
    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

    Twey is right and, these are basically the same solutions offered (correctly) by mwinter and myself earlier in this thread.
    - John
    ________________________

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

  9. #9
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by velanzia
    objectname.onchange = function someFunction() {
    The identifier in the function expression is most likely redundant; an event listener is unlikely to want to call itself, especially without making it like a method call. It is especially important to note that the identifier, someFunction, should be propagated to any other scope outside the body of that function expression. That is to say that 'someFunction' should exist like a local variable within the function.

    The snippet that I posted in my original post is the most likely form that the assignment should take.

    objectname.onchange = someFunction
    Unless there is some other reason to reference the function, someFunction, a separate declaration is unnecessary and should be avoided.


    Quote Originally Posted by Twey
    Actually, you'd have to define the function first.
    If by that you mean:

    Code:
    function someFunction() {
      /* ... */
    }
    
    objectname.onchange = someFunction;
    you are incorrect. They are identical from a behavioural perspective.

    When an execution context is entered (either 'global' code when the script is first run, or the body of a function when that function is called), all function declarations and variable declarations are immediately parsed before execution commences. All functions within scope will be available immediately.

    Though variable declarations are handled straight away, initialisations are deferred until that statement is reached and executed. If the statement is in the block of some control statement, and the condition evaluates to false, the variable will still be declared, but it will not be initialised to the assigned value.

    Mike

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
  •