Solved

How could I save a string as a variable?

Posted on 2014-12-11
4
136 Views
Last Modified: 2014-12-12
Hi all,

I have a bit of wordpress generated code that I would like to change on-the-fly.

The HTML code looks like this:
<div class="javo-item-price-table">
				<ul class="javo-price-item" style="height: 389px;">
				<li class="title">
					<h2>LIST YOUR BUSINESS - YEARLY SUBSCRIPTION</h2>
				</li>
				<li class="price">
					<span style="height: 123px;">
						The price for membership is <strong>£60.00 per Year</strong>. <br>Membership expires after 1 Year.					</span>
				</li>
				<li class="attribute"><span><i class="glyphicon glyphicon-pencil"></i> Item 1 posts</span></li><li class="attribute"><span><i class="glyphicon glyphicon-calendar"></i> Duration 365 days</span></li><li class="attribute disabled"><span><i class="glyphicon glyphicon-thumbs-up"></i> No Featured</span></li>				<li class="submit">
											<a class="pmpro_btn pmpro_btn-select" href="http://www.old-find.com/membership-account/membership-checkout/?level=1">More info</a>
									</li>
			</ul>
						<ul class="javo-price-item" style="height: 389px;">
				<li class="title">
					<h2>LIST YOUR EVENT - YEARLY SUBSCRIPTION</h2>
				</li>
				<li class="price">
					<span style="height: 123px;">
						The price for membership is <strong>£70.00 per Year</strong>. <br>Membership expires after 1 Year.					</span>
				</li>
				<li class="attribute"><span><i class="glyphicon glyphicon-pencil"></i> Item 1 posts</span></li><li class="attribute"><span><i class="glyphicon glyphicon-calendar"></i> Duration 365 days</span></li><li class="attribute disabled"><span><i class="glyphicon glyphicon-thumbs-up"></i> No Featured</span></li>				<li class="submit">
											<a class="pmpro_btn pmpro_btn-select" href="http://www.old-find.com/membership-account/membership-checkout/?level=2">More info</a>
									</li>
			</ul>
						<ul class="javo-price-item" style="height: 412px;">
				<li class="title">
					<h2>LIST YOUR BUSINESS - MONTHLY SUBSCRIPTION</h2>
				</li>
				<li class="price">
					<span style="height: 146px;">
						<strong>£6.00</strong> now and then <strong>£6.00 per Month for 11 more Months</strong>. <br>Membership expires after 12 Months.					</span>
				</li>
				<li class="attribute"><span><i class="glyphicon glyphicon-pencil"></i> Item 1 posts</span></li><li class="attribute"><span><i class="glyphicon glyphicon-calendar"></i> Duration 360 days</span></li><li class="attribute disabled"><span><i class="glyphicon glyphicon-thumbs-up"></i> No Featured</span></li>				<li class="submit">
											<a class="pmpro_btn pmpro_btn-select" href="http://www.old-find.com/membership-account/membership-checkout/?level=5">More info</a>
									</li>
			</ul>
						<ul class="javo-price-item" style="height: 412px;">
				<li class="title">
					<h2>LIST YOUR EVENT - MONTHLY SUBSCRIPTION</h2>
				</li>
				<li class="price">
					<span style="height: 146px;">
						<strong>£7.00</strong> now and then <strong>£7.00 per Month for 11 more Months</strong>. <br>Membership expires after 12 Months.					</span>
				</li>
				<li class="attribute"><span><i class="glyphicon glyphicon-pencil"></i> Item 1 posts</span></li><li class="attribute"><span><i class="glyphicon glyphicon-calendar"></i> Duration 360 days</span></li><li class="attribute disabled"><span><i class="glyphicon glyphicon-thumbs-up"></i> No Featured</span></li>				<li class="submit">
											<a class="pmpro_btn pmpro_btn-select" href="http://www.old-find.com/membership-account/membership-checkout/?level=6">More info</a>
									</li>
			</ul>
</div>

Open in new window


There are two cases where I have strings that look similar:
<strong>£6.00</strong> now and then <strong>£6.00 per Month for 11 more Months</strong>. <br>Membership expires after 12 Months.

Open in new window

and
<strong>£7.00</strong> now and then <strong>£7.00 per Month for 11 more Months</strong>. <br>Membership expires after 12 Months.

Open in new window


I need these to become:
<strong>£6.00 per month for 12 months</strong>. <br>Membership expires after 12 Months.

Open in new window

and
<strong>£7.00 per month for 12 months</strong>. <br>Membership expires after 12 Months.

Open in new window


MY PROGRESS SO FAR:
var monthlySubText = [];
jQuery("ul .price span:contains(' now and then ')").each(function (index) {
    monthlySubText.push(jQuery(this).closest("span").html());
    var price = jQuery("strong",jQuery(this)).first().html();
    jQuery(this).html("<strong>" + price + " per month for 12 months</strong>. <br>Membership expires after 12 Months.");
    });

Open in new window


This works BUT I would like the last part
. <br>Membership expires after 12 Months.
to be passed through a variable too and not typed out as a text string as now.

How could I modify my working jQuery solution to allow this? I need everything withing the span that is after the second </strong> to be saved into a variable and inserted at the end in the last line of the jQuery code. Something like:
$(this).parent().html("<strong>" + txt + " per month for 12 months</strong>" + endTxt);

Open in new window



thanks in advance
0
Comment
Question by:badwolfff
  • 2
4 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 40495920
var monthlySubText = [];
var endTxt = ". <br>Membership expires after 12 Months.";
jQuery("ul .price span:contains(' now and then ')").each(function (index) {
    monthlySubText.push(jQuery(this).closest("span").html());
    var price = jQuery("strong",jQuery(this)).first().html();
    $(this).parent().html("<strong>" + txt + " per month for 12 months</strong>" + endTxt);
    });

Open in new window

0
 

Author Comment

by:badwolfff
ID: 40496038
Hi Leakim thanks for the message. This won't do unfortunately. The string that follows the second </strong> is variable but is always in the format and always in that position. Could that be done?
thanks
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40496087
is variable from what ?
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 40496329
first of all you dont need line 1 & 3

jQuery("ul .price span:contains(' now and then ')").each(function (index) {
    var price = jQuery("strong",jQuery(this)).first().html();
    jQuery(this).html("<strong>" + price + " per month for 12 months</strong>. <br>Membership expires after 12 Months.");
    });

Open in new window


second, here it is:

$("ul .price span:contains(' now and then ')").each(function (index) {
    var price = $("strong", $(this)).first().html();
    var endText = $(this).html().split("<br>")[1];
    $(this).html("<strong>" + price + " per month for 12 months</strong>.<br>" + endText);
});

Open in new window


test here
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Javascript in a user control not working 17 54
SVG Star 4 18
Angular - filter - one works but other doesn't 13 25
Bootstap Icons 3 22
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now