最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change the innerHTML content using JQuery - Stack Overflow

programmeradmin0浏览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 ! :-)

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

Share Improve this question edited Dec 20, 2012 at 7:56 RoR Prog asked Dec 20, 2012 at 7:37 RoR ProgRoR Prog 842 silver badges11 bronze badges 1
  • Also I would suggest using === instead of == stackoverflow./questions/359494/… – Marcel Commented Dec 20, 2012 at 8:08
Add a ment  | 

3 Answers 3

Reset to default 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"
    }

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

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

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)
发布评论

评论列表(0)

  1. 暂无评论