Submit a value in javascript div

hi cant seem to submit the value in this javascript div

<div name="balance" id="win" value="{MIN_BIDS}"><script>
                      
var x = {MIN_BIDS} + ( {MIN_BIDS} * .95 ) ;
document.getElementById("win").textContent = x;
</script></div>

if not how do i go about this, thanks

Hi @skyhighweb I found a couple of things that might be getting in your way but I’m not able to test them right now so take them as suggestions rather than definite fixes.

  1. Try moving the script tag after and outside of the div.
  2. If that doesn’t work try using innerHTML instead of textContent property.

Hope that helps

div elements don’t have a value property, and they will never get submitted with a form – use an input element instead. Also note that {MIN_BIDS} isn’t a valid JS identifier; if you want to obtain that value from an HTML element, you might store it as a data-* attribute, and access it like e.g.

HTML

<div id="win" data-min-bids="42"></div>

JS

var win = document.getElementById('win')
var minBids = win.dataset.minBids
var x = minBids * 1.95

win.textContent = x

BTW, isn’t this the exact same question as in your other recent thread?

<div> elements have neither a name nor a value attribute …

ok so i ended up doing this

<input name="balance" type="text" id="answer" onclick="Calculate();" size="15" maxlength="10" readonly="true">
<input type="text" id="Resources" value="{MIN_BIDS}"/>
<input type="text" id="Minutes"  value="{MIN_BIDS}"/>

<script>
function Calculate()
{

  var resources = document.getElementById('Resources').value;
  var minutes = document.getElementById('Minutes').value; 
  var permin = {MIN_BIDS} + ( {MIN_BIDS} * .95 ) ;
  document.getElementById('answer').value=parseFloat(permin) , parseFloat(minutes);
}

</script>

how do i make it onload not on click, thanks

There are several ways to do this make onload work.

  1. <body onload="calculate()">
  2. Place the JavaScript block before the closing body tag and add a call to the calculate function within the script tags.
  3. Attach onload event via javascript.

I’d avoid option 1 as generally mixing markup and scripts is not regarded as best practice, but I included to give a more complete overview.

However, having taken a look at your latest code above there are the following issues.

  • The balance field is set to readonly, so you wouldn’t be able to click on it and fire of a call to the calculate function.
  • The values of Resources and Minutes fields are both set to ‘{MIN_BIDS}’ they should be numeric values to perform your calculations with.
  • In the JavaScript the permin variable is using {MIN_BIDS} to perform a calculation on however this would cause a reference error.
  • The answer fields value will just be set to the value of permin, if you want to include minutes then use + instead of a comma.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.