Jquery eval variable and convert to currency

0 votes

I have a jquery application where I need to evaluate variables in the form based on how many rows exist in the table.

The desired behavior is to find the selected option of the product, pull out the value (which then corresponds to the pre-defined price based on variable names), then do something with the price. I'm having issues with evaluating the variable name, converting to numbers, then using a currency plugin to convert to currency.

Here is the form:

    <table id="sizetable" class="table table-responsive table-condensed">
    <thead>
        <tr>
            <td width="30%">Shaft Type<input type="hidden" id="numfields" name="numfields" value="6"></td>
            <td width="25%">Team Message (Free)<br><small>Appears on every shaft - 24 character max</small></td>
            <td>&nbsp;</td>
            <td width="25%">Individual Message<br><small>+$5 each - 24 character max</small></td>
            <td>&nbsp;</td>
            <td width="10%">Shaft Price</td>
            <td width="10%"><button class="add_field_button btn btn-success">Add Shaft <span class="glyphicons glyphicons-circle-plus"></span></button></td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><select name="toproduct1" id="toproduct1" class="toproduct">
                    <option value="28">Team Order Specialty Attack Shaft - $25.00</option>
                    <option value="29">Team Order Specialty Defense Shaft - $50.00</option>
                </select>
            </td>
            <td><input type="text" id="teammsg1" name="teammsg1" class="form-control" maxlength="24"></td>
                <td><span class="badge tmsgchr">0</span></td>
            <td><input type="text" id="indmsg1" name="indmsg1" class="form-control indmsg" maxlength="24"></td>
                <td><span class="badge indmsgchr1"></span></td>
            <td><span id="unitprice1"></span></td>
            <td><a class="remove_field btn btn-danger" disabled>Remove <span class="glyphicons glyphicons-circle-minus"></span></a></td>
        </tr>
        <tr>
            <td><select name="toproduct2" id="toproduct2" class="toproduct">
<option value="28">Team Order Specialty Attack Shaft - $25.00</option>
<option value="29">Team Order Specialty Defense Shaft - $50.00</option>
</select>
            </td>
            <td><span class="tmsg light-gray"></td>
                <td></td>
            <td><input type="text" id="indmsg2" name="indmsg2" class="form-control indmsg" maxlength="24"></td>
                <td><span class="badge indmsgchr2"></span></td>
            <td><span id="unitprice2"></span></td>
            <td><a class="remove_field btn btn-danger" disabled>Remove <span class="glyphicons glyphicons-circle-minus"></span></a></td>
        </tr>
...

The jquery I wrote is below:

$(document).ready(function() {  
    var max_fields      = 20; //maximum input boxes allowed
    var min_fields      = 6; //minimum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID
    var ordertot        = 0;
    var indbrandcost    = 5;
    var shaftprice      = 0;

$(".toproduct, .indmsg").on('keyup keypress blur change paste cut', function(){
        var id28 = 25; //dynamically written based on item price
        var id29 = 50;

//loop through all fields, check if that row exists, then see which selection matches up with id28 or 1d29 above, then do something
        for(i = 1; i < max_fields; i++) { 
            if($('#toproduct'+i).val() != '') {
            var f = $('#toproduct'+i).find('option:selected').attr('value');
                if($('#indmsg'+i).val() != '') {
                    $('#unitprice'+i).text((eval('id'+f)+5).currency()); //getting error here
                    //var shaftprice = shaftprice + indbrandcost;
                }
                else {
                    $('#unitprice'+i).text((eval('id'+f)).currency()); // getting error here
                }
            }

        }

    });

Aug 11, 2022 in Web Development by gaurav
• 23,260 points
736 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Web Development

0 votes
1 answer

how to add erroricon and custom validation message using jquery?

By default, the error message is put ...READ MORE

answered Jun 30, 2022 in Web Development by rajatha
• 7,680 points
4,586 views
0 votes
0 answers

convert base64 to image in javascript/jquery

I have written some code for image ...READ MORE

Jun 29, 2022 in Web Development by gaurav
• 23,260 points
3,248 views
0 votes
1 answer

How to set jQuery draggable min/max-left and min/max-right

How to set jQuery draggable min/max-left and ...READ MORE

answered Jul 25, 2022 in Web Development by rajatha
• 7,680 points
667 views
0 votes
1 answer

How to set jQuery draggable min/max-left and min/max-right

How to set jQuery draggable min/max-left and ...READ MORE

answered Jul 25, 2022 in Web Development by rajatha
• 7,680 points
1,166 views
0 votes
0 answers

How to preselect and disable specific checkboxes using js/jquery?

I have checkboxes with randomly generated ids ...READ MORE

Jul 26, 2022 in Web Development by gaurav
• 23,260 points
407 views
0 votes
0 answers

how to spin and stop wheel has custom position with jquery

this i my code. I am using this ...READ MORE

Jul 27, 2022 in Web Development by gaurav
• 23,260 points
1,641 views
0 votes
1 answer

How to evaluate expression given as a string in R?

The eval() function evaluates an expression, but "5+5" is a string, ...READ MORE

answered Jun 7, 2018 in Data Analytics by DataKing99
• 8,250 points
4,746 views
0 votes
1 answer

How to use jquery with asp.net ajax?

If you weren't aware, Microsoft is planning ...READ MORE

answered Oct 15, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
747 views
0 votes
1 answer

Is 'sparkline' a method?

I suggest you to check 2 things That jquery.sparkline.js is actually ...READ MORE

answered Nov 9, 2018 in Apache Spark by Frankie
• 9,830 points
1,270 views
0 votes
1 answer

What is jQuery?

Hey, jQuery is a fast and concise JavaScript ...READ MORE

answered Feb 14, 2020 in JQuery by kartik
• 37,520 points
1,315 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP