10

How Do I get the value of a span tag and send it into my form to another page?

<span id="subtotal"></span>

I need to send the content of my span tag subtotal to another page, I would like to save it into a hidden field, but I found no way to do this..

I used this, but no success!

function getTotal() {
    return alert(document.getElementById('total').innerHTML);
}

Here goes the right function for those who need the answer! After I figured out the script ...

function getTotal() {
    //document.write(document.getElementById('total').innerHTML);
    var someValue = $(".total").text();
    alert("Value is "+someValue);
    //It cause to releoad the page and give me the parameter total          
    location.href="frmAdd.php?total=" + someValue; 
}

4 Answers 4

22

this will give you value

$("#subtotal").text();
8

You could try the .text() method if you're using jQuery.

$("#subtotal").text();
4
  • just kidding , how come your answer is getting voted , i gave answer before yours....
    – kobe
    Nov 25, 2010 at 20:38
  • 1
    @gov: I saw the both answers at the same moment. I found this answer more neat. More correct grammar and punctuation is used. It radiates more professionality.
    – BalusC
    Nov 25, 2010 at 21:17
  • @balusc no problem,I will do that way from next time.
    – kobe
    Nov 25, 2010 at 22:17
  • @gov: I definitely had nothing to do with it, but yes, I always try to be grammatically correct as it does look more professional. =)
    – John
    Nov 25, 2010 at 22:24
4

The value of span is added to the input field of id="sub". you may use any events for assign values to hidden fields like click, change, submit, or you do as your logic .its an option.

$("#sub").val($("#subtotal").text());
<span id="subtotal"></span>
<input type="hidden" id="sub" >
1
  • It was already solved. Using id #subtotal .. anyway thanks for the reply! Apr 2, 2014 at 4:03
2

Should'nt you be having subtotal instead of total there?

function getTotal() {
    return alert(document.getElementById('subtotal').innerHTML);
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.