Umeed Umeed - 1 month ago 6
HTML Question

Trying to calculate amount in jQuery to then insert it into an HTML <p> tag

Background info: I need to take the number of participants registered and multiply it by 11. The CMS internal tag will pull the data for me, I just need to insert the end value that I need automatically without having to change it everytime a new person registers.

HTML:

<table style="width: 100%; border: 1px solid #c94779;" border="0" align="center">
<tbody>
<tr>
<td style="background-color: #c94779; color: #ffffff; padding-left: 14px; padding-top: 8px; width: 100%; height: 28px; font-family: Rockwell; font-size: 1.357em; text-transform: uppercase;">Thank you!</td>
</tr>
<tr>
<td style="vertical-align: top; padding-left: 14px; padding-top: 3px;" valign="top">
<p><span class="subheading">The hard work of our Ambassadors is paying off</span></p>
<p><strong>Total Women Educated:</strong> <strong>[[C1:sponsor_2]]</strong></p>
<p id="womenreached"> </p>
<p><strong>Fundraising Total:</strong> <strong><convio:session name="42" param="0:dollars"></convio:session></strong></p>
<p><strong>Number of Ambassadors:</strong> <strong><convio:session name="42" param="0:num-participants"></convio:session></strong></p>
</td>
</tr>
</tbody>
</table>


JavaScript:

jQuery(document).ready(function() {
var reached = '';
var participants = '[[S42:15793:num-participants]]';
var women = 'participants * 11';
if (women) {
reached = women;
}
else {
reached = 0;
}
jQuery('#womenreached p').html("Total Women Reached: " + reached);
});


The S42 tag I'm using in
var participants
is the custom tag that the CMS system will pull the data with. I noticed it wasn't working at all when the part "Total Women Reached: " wasn't being inserted at all.

I tried to do it in jsfiddle and replaced the s42 tag with
var participants = '600'
and it doesn't work either. I tried to read the document but I can't figure out what I'm doing wrong.

Any help would be greatly appreciated!

Thanks

Answer

The problem is the quotes. Assuming the CMS value is numerical, remove the quotes around the participants value and also around the calculation you're doing.

Also note that your if statement is pretty redundant too, as the value will already be 0 in the else case when you set it to 0.

Try this:

$(document).ready(function() {
   var participants = [[S42:15793:num-participants]]; // assuming this value comes from server
   var women = participants * 11;
   $('#womenreached').text("Total Women Reached: " + women);
});

Working example:

var participants = 600;
var women = participants * 11;
console.log("Total Women Reached: " + women);

Comments