1

This is the javascript snippet which assigns the selected option value to a text box

function check() {
 document.getElementById("inp").value=document.getElementById("id").value ;
} 

this is the actual html code

<form>
<select id="id"  onSelect="check( );">
<option></option>
<option value=85.5>opt1</option>
<option value=95.5>opt2</option>
<option value=95.5>opt3</option>
<option value=95.5>opt4</option>
</select>
<input type=text name="input" id="inp">
</form>

I Hope am done right ,but still am not able to get my solution

3 Answers 3

1

Here's your fix.

This one works

I guess the summary of it is that you should change onSelect to onChange.

<script>
 function check() {
     document.getElementById("inp").value=document.getElementById("id").value;
    } 
 </script>
</head>
<body>
    <form>
        <select id="id"  onChange="check();">
        <option></option>
        <option value=85.5>opt1</option>
        <option value=95.5>opt2</option>
        <option value=95.5>opt3</option>
        <option value=95.5>opt4</option>
        </select>
    <input type=text name="input" id="inp" value="">
    </form>
</body>
Sign up to request clarification or add additional context in comments.

5 Comments

and do onChange="check(this)" so you can do function check(theSel) { theSel.form.input.value=theSel.value } if it must be inline
Not to mention that all of this could be handled much more elegantly with jQuery altogether
Yes. Exactly. jQuery all the things
So we load a framework to be able to do $(function() { $("#id").on("change",function() { $("#inp").val($(this).val()) }); }); instead of window.onload=function() { document.getElementById("id").onchange=function() { this.form.input.value=this.value }} I am not really convinced that is necessary
Depends, usually there's a lot more javascript going in a site than what we see in this little snippet. I'm sure there would be a lot more if we could see the full site. If, however, that's the only place then yeah I wouldn't recommend jQuery either
1

Assuming this html

<select id="id">
  <option value="">Please select</option>
  <option value="85.5">opt1</option>
  <option value="95.5">opt2</option>
  <option value="95.5">opt3</option>
  <option value="95.5">opt4</option>
</select>

You can use unobtrusive JavaScript and add this to the head

Plain JS:

window.onload=function() { 
  document.getElementById("id").onchange=function() {     
    this.form.input.value=this.value; 
  }
}

Same in jQuery would be

$(function() { 
  $("#id").on("change",function() { 
    $("#inp").val($(this).val()); 
  }); 
}); 

If you MUST do it inline, then

<select id="id" onchange="this.form.input.value=this.value;">

Comments

0

You HTML has multiple issues as well as your javascript

Your formatted HTML

<form>
<select id="id" onChange="check();">
    <option></option>
    <option value="85.5">opt1</option>
    <option value="95.5">opt2</option>
    <option value="95.5">opt3</option>
    <option value="95.5">opt4</option>
</select>
<input type="text" name="input" id="inp" />
</form>

JS

function check() {
    document.getElementById("inp").value = document.getElementById("id").value;
}

Working DEMO

Also you should enclose you values like value="". Besides this no major issues

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.