-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,503
  • 3
  • 15
  • 15

8 Answers8

4

Try this:

document.getElementById('txtScore').value = totalScore;
Julian Camilleri
  • 2,975
  • 1
  • 25
  • 34
CodeSlayer
  • 1,318
  • 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,848
  • 1
  • 18
  • 21
1
document.getElementById("id").value =your value;

By using this you can assign value to textbox.

PSR
  • 39,804
  • 41
  • 111
  • 151
1

Pure JavaScript:

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

jQuery:

$('#myTextBox').val('myValue');
Jason Watmore
  • 4,521
  • 2
  • 32
  • 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
  • 27,065
  • 8
  • 76
  • 78
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,826
  • 1
  • 21
  • 40
1

Here is the jQuery syntax

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

or in HTML

document.getElementById('yourTextboxID').value=totalScore;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
LittleDragon
  • 2,317
  • 2
  • 18
  • 23
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
  • 156
  • 2
  • 15
CodeSlayer
  • 1,318
  • 1
  • 12
  • 34