nodeffect
nodeffect

Reputation: 1840

Adding and Subtracting values using javascript to HTML label or text field

I need another help over here. I'm able to add and subtract to "textbox" field but not on "label" or "text" fields. I would like to change the value of the text base on my checkboxes. Here's my code down below.

<div style="margin-bottom:10px;">
<label>CPV (RM)<strong style="margin-left:100px; color:red;" id="CPV">0.05</strong></label>
    <label>CPC (RM)<strong style="margin-left:100px; color:red;" id="CPC">0.55</strong></label>
    <input type="text" name="CPV"/>
    </div>

    <div style="margin-bottom:10px;">
    <label style="float:left;">Ad Targeting</label> 
    <div id="ad-target-button" style="margin-left:85px;" onclick="toggle()">
    <input type="checkbox" checked="checked" value="On" name="adtarget" id="adtarget_check">
    </div>
    </div>

    <div class="row-fluid">
    <label style="float:left; margin-top:5px;">Target Gender</label>
    <div style="display:block;">
    <label class="checkbox inline">
    <input type="checkbox" value="male" id="gender_male" name="gender[]" class="gender" onclick='add(922.63, this);'/>Male
    </label>
    <label class="checkbox inline"><input type="checkbox" value="female" id="gender_female" name="gender[]" class="gender" onclick='add(400.3, this);/>Female
    </label>
    </div>
    </div>

Here's the javascript function to calculate...

function correctNumber(number) {
    return (parseFloat(number.toPrecision(12)));
}

function add(total, this_chk_bx) {
    var CPV = (page3.CPV.value * 1);
    var total = total * 1;

    if (this_chk_bx.checked == true) {
        //add if its checked
        page3.CPV.value = correctNumber(CPV + total);
        $('#CPV').text(val)  = correctNumber(CPV + total);
    } else {
        //subtract if its unchecked
        page3.CPV.value = correctNumber(CPV - total);
        $('#CPV').text(val)  = correctNumber(CPV - total);
    }
}

I've get the code from this JSFIDDLE: http://jsfiddle.net/kHxmG/5/ But I don't want it to display the value in a textbox. I want it to display it in a <label>.

Upvotes: 0

Views: 1798

Answers (1)

Lotus
Lotus

Reputation: 449

Your id #CPV is not a label. The strong-Tag has no method .text() so you can't add a text like that.

You can change the text of a label like this:

document.getElementById('myLabel').innerHTML = sum;

Working fiddle: http://jsfiddle.net/kHxmG/19/

Upvotes: 2

Related Questions