Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a variable from a javascript function into html body

I am still new to javascript, and I am trying to get a function to return a variable using html & javascript. Basically the function should just return whichever radio button that the user clicks on, although at the moment I don't see anything being returned at all.

The function is here:

<script type="text/javascript">
function GetSelectedItem() {
var chosen = ""
len = document.f1.r1.length
  for (i = 0; i <len; i++) {
    if (document.f1.r1[i].checked) {
chosen = document.f1.r1[i].value
    }
  }
}
return chosen
</script>

And then in the html section I have these radio buttons, and my attempt to get the variable "chosen" output to the screen.

  <form name = f1><Input type = radio Name = r1 Value = "ON" onClick=GetSelectedItem()>On
  <Input type = radio Name = r1 Value = "OFF" onClick =GetSelectedItem()>Off</form>
  <script type ="text/javascript">document.write(chosen)</script>

At the moment nothing seems to be getting returned from the function (although if I output the variable 'chosen' inside the function then it is working correctly.

Thanks in advance!

like image 206
anthr Avatar asked Jan 16 '11 17:01

anthr


People also ask

Can you return a variable from a function JavaScript?

JavaScript passes a value from a function back to the code that called it by using the return statement. The value to be returned is specified in the return. That value can be a constant value, a variable, or a calculation where the result of the calculation is returned.

How do you return a variable from a function?

To return a value from a function, you must include a return statement, followed by the value to be returned, before the function's end statement. If you do not include a return statement or if you do not specify a value after the keyword return, the value returned by the function is unpredictable.


3 Answers

Here's a little simpler approach.

First, make a few corrections to your HTML, and create a container to display the output:

<form name = "f1"> <!-- the "this" in GetSelectedItem(this) is the input -->
    <input type = "radio" Name = "r1" Value = "ON" onClick="GetSelectedItem(this)">On
    <input type = "radio" Name = "r1" Value = "OFF" onClick ="GetSelectedItem(this)">Off
</form>

<div id="output"></div>

Then change your script to this:

<script  type="text/javascript">
         // Grab the output eleent
    var output = document.getElementById('output');

       // "el" is the parameter that references the "this" argument that was passed
    function GetSelectedItem(el) {
        output.innerHTML = el.value; // set its content to the value of the "el"
    }
</script>

...and place it just inside the closing </body> tag.

Click here to test a working example. (jsFiddle)

like image 191
user113716 Avatar answered Oct 19 '22 05:10

user113716


document.write takes a string, and outputs it as part of the HTML. This is not a live value that updates when the variable pointing at the string is updated.

For that, you will need to perform DOM manipulation.

like image 32
Quentin Avatar answered Oct 19 '22 06:10

Quentin


Change your JavaScript function to something like that:

<script type="text/javascript">
function GetSelectedItem() {
  len = document.f1.r1.length;
  for (i = 0; i <len; i++) {
    if (document.f1.r1[i].checked) {
      document.getElementById('test').textContent = document.f1.r1[i].value;
    }
  }
}
</script>

And then in the body:

<div id="test"></div>
like image 2
Martin Buberl Avatar answered Oct 19 '22 06:10

Martin Buberl