0

i want to add 500 using onclick function in span 1000 means onclick at check box sapn value will be 1500

JS

function add() {
    if (document.Form1.checkbox1.checked == true) {
        // what will code here to sum 500  in to 1000   
    }
}

HTML

<form name="Form1" style="color: green ; font-size: 150%" action="#">
    <input type="checkbox" name="checkbox1" onclick="add()" />500
    <br />
    <span>1000</span>
</form>
5
  • first give id or class to that span Commented Sep 21, 2015 at 6:44
  • var spnVal = $("#spnId").text() or $("#spnId").val() ; Commented Sep 21, 2015 at 6:44
  • var newVal = parseInt(spnVal )+500; Commented Sep 21, 2015 at 6:45
  • $("#spnId").text(newVal); or $("#spnId").val(newVal); Commented Sep 21, 2015 at 6:45
  • @KushalVora Maybe, OP wants to add the value from the textbox to the span Commented Sep 21, 2015 at 6:47

6 Answers 6

3

Make sure to add an id to your span element so you can easily target it in javascript. Then target the element and change the innerHTML.

var spanToChance = document.getElementById("#spanID");
spanToChange.innerHTML = parseInt(spanToChange.innerHTML) + 500; 
Sign up to request clarification or add additional context in comments.

Comments

1

If you don't want to use jQuery, then the the following can be used.

function add(element) {
  var form = document.getElementsByName("Form1")[0];
  var val = form.getElementsByTagName('span')[0].innerHTML;
  if (element.checked == true) {
    form.getElementsByTagName('span')[0].innerHTML = parseInt(val) + parseInt(element.value);
  } else {
    form.getElementsByTagName('span')[0].innerHTML = parseInt(val) - parseInt(element.value);
  }
}
<form name="Form1" style="color: green ; font-size: 150%" action="#">
  <input type="checkbox" name="checkbox1" onclick="add(this)" value="500" />500
  <br />
  <span>1000</span>
</form>

Comments

0
  • You can use jquery to bind change event to the checkbox instead of writing inline function calls.
  • Use the value attribute for checkbox in-order to provide the value.
  • Use parseInt before adding the contents. Otherwise it will take it as string

Example code

   $("[name='checkbox1']").change(function () {
        var spanvalue = parseInt($("span").text().trim());
        if (this.checked) {
            spanvalue = spanvalue + parseInt(this.value);
        } else {
            spanvalue = spanvalue - parseInt(this.value);
        }
        $("span").text(spanvalue);
    });

Fiddle

Comments

0

As Kushal has mentioned in their four comments, you need to add an identifying feature to the span so you know where to put the new value.

You can then use document.getElementById() (if in javaScript) or $('#id') (in jQuery) to access this element and change the text (via innerText/innerHTML in js and .text()/.html() in jQuery)

Comments

0
<span id="myspan"> 1000 </span>

try this code -

document.getElementById("myspan").innerHTML="1500";

for modern browser-

document.getElementById("myspan").textContent="1500";

Comments

0

Please try the bellow code,

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	$('#checkbox1').click(function(){
		var checkboxval = parseInt($(this).val());
		var spanval     = parseInt($('#total').text());
		if(this.checked){
			$('#total').text(checkboxval + spanval);
		}
		
	});
});
</script>
<body>
<form name="Form1" style="color: green ; font-size: 150%" action = "#">

<input type="checkbox" name="checkbox1"  id="checkbox1" value="500"/>500

<br/>

<span id="total">1000</span>

</form>

</body> 

Let me know if you have any query

Thanks

1 Comment

but after unchecked it will be 1000 please help

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.