views:

224

answers:

3

Hi please help me, I have some textarea data which needs to be changed when the user select different option from a <select>.

can you please give me some javascript easy code to do that

please i don't want jquery things

A: 

Why don't you want to use jQuery? It's about 10 times easier, not just to use but also to debug and it has alot of community support

Rakward
hi RakwardThank you for your reply, but am facing a cross browser issue with jquery codes to make this thing.if you can advise about jquery code that works perfectly on all browsers that would be great
xyro
jQuery should be included as part of JavaScript core in it's next release :P
Matt
+3  A: 

You can use the onchange event. I can't write the code for you because I don't know what your current code looks like (and that's not really what this site is about), but I can give you an example:

var select = document.getElementById("mySelect");
select.onchange = function ()
{
    // get a reference to the textarea element
    var tArea = document.getElementById("myTextArea");

    // Set the text to the value of the currently selected option
    tArea.value = this.options[this.selectedIndex].value;
}
Andy E
A textarea is an input field, you don't need to try to replace its text content (and shouldn't, since input fields' DOM content is different from their user-input-value, except in IE due to a bug). Just `tArea.value= this.options[this.selectedIndex].value` is fine. (You probably could do away with the options lookup and just `this.value` too, unless you need to support ancient browsers.)
bobince
@bobince: Thanks :-) I knew that worked in IE but I wasn't sure if it was x-browser or not. I checked after posting my answer and edited it in. `this.value` would be fine, but I'd rather avoid the "legacy-browser-supporting-downvote-patrol", whom I've bumped into on a few occasions ;-)
Andy E
Will `this` work in this case? Doesn't IE like attaching `this` to the main window?
SeanJA
In a traditional-events handler as in `onchange= ...`, `this` will always be the element in question. It's IE's own `attachEvent` event model that typically forgets to bind `this`.
bobince
A: 

Try Following it may help...........

< script type="text/javascript">
    functionName(this){
    var tArea = document.getElementById("myTextArea");
    if(this.value=="1"){
    tArea.value="1"
    }else if(this.value=="0"){
       tArea.value="0"
  }
< /script>

< select onchange="functionName(this);">
    < option value="1">Available< /option>
    < option value="0">  Not Available< /option>
 < /select>
Salil
some formatting would be good (4 spaces infront)
SeanJA
@SeanJA select all - CTRL-K
Sky Sanders
@SeanJA Sorry n thanx....
Salil
@Sky_Sanders: I don't have the ability to edit other user's replies (yet?)
SeanJA