SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    change the "onchange" attribute

    I've got this code for a test script:
    Code:
    function change(o) {
    var newSelect, i;
    newSelect=document.createElement('select');
    for(i=0; i<5; i++)
    {
    	newSelect.options[i] = new Option(i,i);
    }
    newSelect.size="4";
    newSelect.onchange=change(this);
    o.onchange="";
    o.parentNode.appendChild(newSelect);
    }
    What i'm trying to do is to create a Select object next to a select object when the value of the original select is changed. The problem I'm facing is that I want the created Select to do the same thing when its value is changed. With the above code, i get a "too much recursion" error. I tried changing it so that the function did not take any parameters and just used 'this' instead of 'o', and changing
    Code:
    newSelect.onchange=change(this);
    to
    Code:
    newSelect.onchange=change;
    but that breaks it with error that "this.parentNode has no properties".

    And thus I come to you, oh gurus of Javascript, for help. Any ideas?

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try
    Code:
    newSelect.onchange = function() { change(this); }

  3. #3
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you're my hero. thanks!


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
  •