0

When I select any option in list then it should print its value in textbox(all html).

enter image description here

I tried

stafflist.setAttribute("onchange", "javacript:document.getElementById('id_17_enrolpassword').value = this.value;");

Its working in IE8+ and all modern browsers but not in IE7.

Also tried

stafflist.addEventListener('onchange',"javacript:document.getElementById('id_17_enrolpassword').value = this.value;",false);

So what changes I should do here?

OammieR
  • 2,800
  • 5
  • 30
  • 51
parthiv777
  • 22
  • 5
  • The `.addEventListener()` function isn't implemented by IE until version 9, as [explained by MDN](https://developer.mozilla.org/en/DOM/element.addEventListener#Legacy_Internet_Explorer_and_attachEvent) (and if you read that doco page you'll see that even for browsers that do implement it you've got the syntax wrong: the second param should be a function, not a string). – nnnnnn Jun 26 '12 at 07:01

4 Answers4

1

IE only fires the onchange event when the element loses focus - if you were to click outside the element or tab to a different element it should fire then.

You can get around this by using a different even, for example onkeypress

Gaurav Agrawal
  • 4,355
  • 10
  • 42
  • 61
  • As predictable, the issue was not the code, but rather IE. An interesing detail I'd never know, +1 – WhyNotHugo Jun 26 '12 at 06:57
  • _"IE only fires the onchange event when the element loses focus"_ - except that the OP says it works in IE8+. @Hugo - note that the second example in the question won't work in _any_ browser. – nnnnnn Jun 26 '12 at 07:06
0

do it this way -

stafflist.onchange = function(){
   document.getElementById('id_17_enrolpassword').value= this.value;
}
Kshitij
  • 8,474
  • 2
  • 26
  • 34
0

1) the javascript: label is only needed if the first script on the page is vbscript.

2) does this work better?

document.getElementById('stafflist').onchange=function(){
  document.getElementById('id_17_enrolpassword').value = this.value;
}

?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

I know this doesn't truly answer the question at hand, but, can't you use something like jQuery to code these sort of even handlings?

The code is a bit more readable (IMHO), and you don't have to deal this these cross-browser scripting issues yourself.

WhyNotHugo
  • 9,423
  • 6
  • 62
  • 70
  • i know i can make it work easier with Jquery ,but I am editing Moodle (open source php made E-learning)...they mentioned that they don't recommend much to use jquery as they used YUI JavaScript library ,anyways i will try to include it... – parthiv777 Jun 26 '12 at 07:14
  • Well then, this might help: http://stackoverflow.com/questions/7039787/onchange-in-yui – WhyNotHugo Jun 26 '12 at 07:16