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 ! :-)

1

3 Answers 3

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"
    }
Sign up to request clarification or add additional context in comments.

Comments

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()

Comments

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)

4 Comments

ALWAYS make a fiddle before posting a question in javascript :)
OP also appears to be missing a closing bracket } at the end of his code. Might be nice to add to the answer.
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 ?
ok....thanx adil & everybody here for such a quick response to solve my prob, i was stuck since yesterday. Thanks everyone....

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.