0

Want to hone in on my javascript. I want text from select-option dropdown sent to an input. ive found a way to do it.. but my question is- is there a shorter way (in vanilla javascript)?

<select id="gren" onChange="return func1(this)"><option>1</option><option>2</option>
<option>3</option><option>4</option></select>
<input style="text" id="testing">

with

<script>
function func1(select){
var rr = document.getElementById("gren");
var jest = rr.options[rr.selectedIndex].text;
document.getElementById("testing").value = jest; }
</script>

i was thinking something along the lines of:

function func1(select){document.getElementById("testing").value = 
this.options[select.selectedIndex].innerHTML;}

but it's not working.. why?

1
  • innherHTML = innerHTML. And what is not working? Errors in console? Commented Jan 17, 2014 at 11:54

5 Answers 5

2
<select id="gren" onChange="return func1(this)"><option>1</option><option>2</option>
<option>3</option><option>4</option></select>
<input style="text" id="testing">
<script>
function func1(select){document.getElementById("testing").value = 
select.value;}
</script>

OR

<select id="gren" onChange="return func1(this)"><option>1</option><option>2</option>
<option>3</option><option>4</option></select>
<input style="text" id="testing">
<script>
function func1(select){document.getElementById("testing").value = 
select.innerHTML;}
</script>
Sign up to request clarification or add additional context in comments.

Comments

2

Shorter isn't always better, code clarity and readability is what you want - as well as good structure. If you want to improve your JavaScript then start by never using inline attributes for event handling, such as onchange.

Here's my jsFiddle and code below:

HTML:

<select class="mySelect">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
</select>
<input type="text" class="myInput">

JavaScript:

var select = document.querySelector('.mySelect');
var input = document.querySelector('.myInput');
var updateInput = function () {
    input.value = this.options[select.selectedIndex].innerHTML;
};
select.onchange = updateInput;

1 Comment

@user3108832 Google around you'll find many reasons, the main being too tightly coupling HTML+JS, as well as the fact you're polluting the global scope with functions to make them accessible in the DOM. Plus this is 90's style coding, not 2014.
1

Try select variable instead of this keyword

function func1(select) {
    document.getElementById("testing").value = select.options[select.selectedIndex].innherHTML;
    //OR
    document.getElementById("testing").value = select.options[select.selectedIndex].text;
}

Comments

1

You have wrong this using and syntax error:

function func1(select){document.getElementById("testing").value = 
**this**.options[select.selectedIndex].inn**h**erHTML;}

this is window in your code.

Try this:

 function func1(select){document.getElementById("testing").value = 
**select**.options[select.selectedIndex].innerHTML;}

Comments

0

Try this,

 function func1(select){
      document.getElementById("testing").value = select.text;
  }

Comments

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.