0

My goal to retrieve value from javascript and SHOW IN INPUT. I am able to view it on span BUT NOT input .

Below are my codes. Help will be appreciate! :)

<SCRIPT type="text/javascript">
function GetSelectedItem()
{
    var e = document.getElementById("staff");
    var strSel =  e.options[e.selectedIndex].value;
    alert(strSel);
    $('#inputId').text(strSel);
}


</SCRIPT>

<input id="inputId">
1
  • Java is to JavaScript as Ham is to Hamster. Commented Jan 13, 2014 at 9:50

3 Answers 3

3

First you need to have an unique ID for each element. Then you can set the value like this:

$("#spanId").val(strSel);

UPDATE:

$("#inputId").val(strSel);
Sign up to request clarification or add additional context in comments.

1 Comment

I have edited my question. I want to display in an input. Not span. Sorry for the inconvenience caused.
1

For inputs, you have to change the value:

$('#inputId').val(strSel);

2 Comments

I have edited my question. I want to display in an input. Not span. Sorry for the inconvenience caused.
@newbieinjavaversion2 All you really did to change the question was change the ID. Because it's an input, you need to use the .val function to set the value. The # in the jQuery selector means "find something with the following id."
1

why you are mixing JS and jQuery together? Do one of these:

var e =  document.getElementById("staff");  

could be directly

var strSel = ('#staff').val(); 
$('#spanId').val(strSel);

OR by purely JS you can write:

 document.getElementById("spanId").value = strSel;

2 Comments

I have edited my question. I want to display in an input. Not span. Sorry for the inconvenience caused.
No problem... $('#inputId').val(strSel) OR document.getElementById("inputId").value = strSel; just the ID change ..

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.