-1

I have a variable called totalScore:

totalScore = '<h2>Total Score: ' + parseInt(parseFloat(scorePrompt) +
                                                        parseFloat(scorePrompt2) +
                                                        parseFloat(scorePrompt3) +
                                                        parseFloat(scorePrompt4) +
                                                        parseFloat(scorePrompt5) +
                                                        '</h2>');

I need to pass the number stored inside totalScore to a textbox. I have not found a way to pass a variable to an html element, sorry if I am missing something very obvious I am new to javascript.

Frustrated Python Coder
  • 1,423
  • 3
  • 14
  • 15

8 Answers8

4

Try this:

document.getElementById('txtScore').value = totalScore;
Julian Camilleri
  • 2,582
  • 1
  • 22
  • 33
CodeSlayer
  • 1,295
  • 1
  • 12
  • 34
2

to give you some background on why we do what we do:
javascript gets its hooks into html via the DOM API. this accounts for most uses of js. what you are trying to do is two things: add an element to the html, and insert a text node inside that element. one doesn't really 'pass a variable' to an html element. one can turn a variable into a string, and append that, as a text node, to the DOM. so you might be looking for something like this:

totalScore = '<h2>Total Score: ' + parseInt(parseFloat(scorePrompt) +
parseFloat(scorePrompt2) +
parseFloat(scorePrompt3) +
parseFloat(scorePrompt4) +
parseFloat(scorePrompt5) +
'</h2>');
document.getElementById("myelementID").innerHTML = totalScore;
danyamachine
  • 1,629
  • 1
  • 16
  • 21
1
document.getElementById("id").value =your value;

By using this you can assign value to textbox.

PSR
  • 38,073
  • 36
  • 106
  • 149
1

Pure JavaScript:

document.getElementById('myTextBox').value = 'myValue';

jQuery:

$('#myTextBox').val('myValue');
Jason Watmore
  • 4,362
  • 2
  • 30
  • 36
1

Just add this line of code

document.getElementById("{yourTextBoxId}").value= parseInt(parseFloat(scorePrompt) +
                                                        parseFloat(scorePrompt2) +
                                                        parseFloat(scorePrompt3) +
                                                        parseFloat(scorePrompt4) +
                                                        parseFloat(scorePrompt5);

just replace {yourTextBoxId} to id of your textbox.

By doing this you will get the number without html.

Kirsteins
  • 26,489
  • 8
  • 74
  • 77
Ashish
  • 21
  • 2
1

try something like this

    totalScore = parseInt(parseFloat(scorePrompt) +
                    parseFloat(scorePrompt2) +
                    parseFloat(scorePrompt3) +
                    parseFloat(scorePrompt4) +
                    parseFloat(scorePrompt5);
    // if you want only number.
    document.getElemenyById('text_box_id').value = totalScore;

    // if you want number + string.
    totalScore = '<h2>Total Score: ' + totalScore + '</h2>');
    document.getElemenyById('text_box_id').value = totalScore;
rajesh kakawat
  • 10,698
  • 1
  • 20
  • 39
1

Here is the jQuery syntax

$('#yourTextboxID').val(totalScore)

or in HTML

document.getElementById('yourTextboxID').value=totalScore;
marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
LittleDragon
  • 2,127
  • 2
  • 16
  • 21
0

why not try to put another variable..like this

var total =parseInt(parseFloat(scorePrompt) +                                                     parseFloat(scorePrompt2)+
                   parseFloat(scorePrompt3) +
                   parseFloat(scorePrompt4) +
                   parseFloat(scorePrompt5);

totalScore = '<h2>Total Score: ' + total +'</h2>';

//now you don't need to cut every character to check if it is numeric or not
document.getElementById('txtValue').value=total;
Wielder
  • 206
  • 2
  • 14
CodeSlayer
  • 1,295
  • 1
  • 12
  • 34