Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select onchange event doesn't fire

Status
Not open for further replies.

tomih

Programmer
Sep 23, 2003
13
0
0
SI
Hi!

I have a problem when I programaticaly define onchange event to select tag.

the working code looks like this:
Code:
var d = document;
//create select element
var tmp = &quot;<select id='selectID' onchange='alert()'>&quot;;
var s = d.createElement(tmp);
//append select element to a div on page
div.appendChild(s);

non-working code:
Code:
var d = document;
//create select element
var tmp = &quot;<select id='selectID'>&quot;;
var s = d.createElement(tmp);
//append select element to a div on page
div.appendChild(s);
selectID.onchange = &quot;alert()&quot;;
//or
s.onchange = &quot;alert()&quot;;
//or
s.onchange = function {alert()};

I need to define onchange event programmaticaly.

I use IE 6.

Can anybody help,
thnx,
T.

Tomi Hrovatin
 
Slight adaptation of your code:
Code:
window.onload = function() {

  var d = document;
  var div = document.getElementsByTagName(&quot;div&quot;)[0];

  //create select element
  var tmp = &quot;<select id='selectID'>&quot;;
  var s = d.createElement(tmp);

  //append select element to a div on page
  div.appendChild(s);
  s.onblur = function() {
    alert('test');
  };

}
 
Well, yes and thanx for taking me on the right track. I realy needed a break to see this mistake:

The wrong part was:
Code:
s.onchange = &quot;function () {alert()}&quot;;

but should be:

Code:
s.onchange = function () {alert()};

thnx again,

t.

Tomi Hrovatin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top