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?

1
  • The .addEventListener() function isn't implemented by IE until version 9, as explained by MDN (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). Commented Jun 26, 2012 at 7:01

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

As predictable, the issue was not the code, but rather IE. An interesing detail I'd never know, +1
"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.
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;
}

?

Comments

0

do it this way -

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

1 Comment

i tried this one as well ,but sorry to say not working (not working with any browsers)...
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.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.