0
var user;
(function (user) {
    user = {
        dynamicPriceChange: function () {
            $("input[name='user[plan_id]']").change(function (e) {
                var planId = $(this).data('text');
                var durationPlan = $('p#durSubsMY')[0].innerHTML;
                var price = $('.price')[0].innerHTML;
                $('.price').text(planId);
                $('span#Love')[0].innerHTML = price;
                if price == "29" {
                    durationPlan = "per month";
                }
                if price == "261" {
                    durationPlan = "per year";
                }
            });
        }
        jQuery(function () {
            user.dynamicPriceChange();
        });
    })(user)

I'm trying to change durationPlan as "per month" if the price is 29 & "per year" if the price is 261.
But I'm unable to change that. Please help, I'm new to jQuery.

The Working Corrected Code is

    if (price == "29") {
         $('p#durSubsMY')[0].innerHTML = "per month"
        }
    if (price == "261" ){
         $('p#durSubsMY')[0].innerHTML = "per year"
        }

Thanks everybody for your help !!!

Cheers ! :-)

RoR Prog
  • 84
  • 2
  • 11
  • Also I would suggest using === instead of == http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use – Marcel Dec 20 '12 at 08:08

3 Answers3

2

Wrong syntax of if statement, you missed the if condition part parenthesis.

Change

if price == "29" {
       durationPlan = "per month"
}

if price == "261" { durationPlan = "per year" }

To

if (price == "29") {
      durationPlan = "per month"
}
if (price == "261" ){
     durationPlan = "per year"
}

One of closing bracket is also missing in the end.

Change

})(user)

To

}})(user)
Adil
  • 146,340
  • 25
  • 209
  • 204
  • 2
    ALWAYS make a fiddle before posting a question in javascript :) – povilasp Dec 20 '12 at 07:44
  • OP also appears to be missing a closing bracket `}` at the end of his code. Might be nice to add to the answer. – Cerbrus Dec 20 '12 at 07:47
  • Adil its effecting my other functionality, i mean that i have written few more functions whose functionality got affected by writing this, how do i keep them unchanged. Sorry but how to get everything working ? – RoR Prog Dec 20 '12 at 07:48
  • ok....thanx adil & everybody here for such a quick response to solve my prob, i was stuck since yesterday. Thanks everyone.... – RoR Prog Dec 20 '12 at 07:51
2

durationPlan is just a variable that contains the innerHTML of the element changing its value does not alter the elements content. Try instead

    if (price == "29") {
        $('p#durSubsMY')[0].innerHTML = "per month"
    }
    else if (price == "261") {
        $('p#durSubsMY')[0].innerHTML = "per year"
    }
Musa
  • 96,336
  • 17
  • 118
  • 137
2

As #Adil mentioned, you forgot you parenthesis in the if statement. Also you can try html() function from the jQuery library http://api.jquery.com/html/ instead of innerHTML.

var planId = $(this).data('text'); if you want to get the value from the input use var planId = $(this).val()

Andrei Stalbe
  • 1,511
  • 6
  • 26
  • 44