The other thing that's not too important, but I'd like to know if its possible to remove any text that starts with a certain string. Looking at the same link above, I would like to remove all the breed guidelines in the tables, so text that starts with 'breed guidelines'.

I'm very much a js and jquery rookie so any help would be much appreciated!!!

Thank you,

James

spoilt
—
2012-12-03T14:19:47Z —
#2

Hello everyone,

I know it's quite an unusual request, but I'd love to know if this is even possible and if someone could steer me in the right direction so I can get started on implementing this.

I really hope someone can help :)))

Thanks very much in advance!

James

Paul_Wilkins
—
2012-12-03T20:00:14Z —
#3

spoilt said:

Hello everyone,

I know it's quite an unusual request, but I'd love to know if this is even possible and if someone could steer me in the right direction so I can get started on implementing this.

I really hope someone can help :)))

The reason why you may not be getting much help with this is that changes to the content are normally better achieved from the backend that's serving the data instead.

To resolve your issue though, it seems that the most important piece of the puzzle is to come up with a function that accepts the sizing string, and returns an appropriate index number for how large it is.

So XS or XSmall or X small or x-small or x-s, or such variations would result in 0, large would be 4, and superdog 2 would be 8

I'll take a look at what can be done with that in a few hours.

spoilt
—
2012-12-04T00:15:36Z —
#4

paul_wilkins said:

The reason why you may not be getting much help with this is that changes to the content are normally better achieved from the backend that's serving the data instead.

To resolve your issue though, it seems that the most important piece of the puzzle is to come up with a function that accepts the sizing string, and returns an appropriate index number for how large it is.

So XS or XSmall or X small or x-small or x-s, or such variations would result in 0, large would be 4, and superdog 2 would be 8

I'll take a look at what can be done with that in a few hours.

Paul that sounds great!

Thanks for replying, let me know how you get on

James

Paul_Wilkins
—
2012-12-04T11:26:25Z —
#5

Today ended up being busier than expected, but I'm back to things now.

Here's a basic start, where we attempt to use a regex to capture common parts from the size string.

The reason why it's showing the wrong size is due to regular expressions being greedy by default. The first (.*) capture group grabs the whole string, but the next capture group then has nothing, so the regex removes one character from the first capture group and looks for a match from the second capture group, which it finds with the 'l' character from the end of the word "small".

That's not how we want the regex to work. Instead of the regex being greedy, we want the first capture group to be lazy instead. We can do that by adding a question mark after the asterisk, so that we instead end up with (.*?)

var sizeRx = /(.*?)(small|medium|large|super|s|m|l)(.*)/,

Now we end up getting the right size for each test.From here it's just a matter of turning the size in to a numeric number for ordering, and of dealing with special situations.

We can use a simple switch statement to provide different size values:

I'm trying my best to understand it, thanks for explaining as you go through so I can actually try to grasp an understanding too.

Am I right in saying that this code will get the sizes to equal numbers, but it won't actually reorder the table yet? Is there another step still to do..? Sorry for my (very) minimal understanding..

spoilt
—
2012-12-04T13:03:36Z —
#7

sorry - you did explain what the code would output.so would i then hide the original table.. and be able to add styling to the output html, or insert it into a new table..?thanks again

Paul_Wilkins
—
2012-12-04T20:08:02Z —
#8

spoilt said:

sorry - you did explain what the code would output.so would i then hide the original table.. and be able to add styling to the output html, or insert it into a new table..?thanks again

The original table can remain, for a simple way to reorder the table is to loop through those difference sizes in numerical order, from biggest to smallest, and move each row to the top of the table. That way as you work your way from the largest ones down to the smallest ones, the table ends up having the smallest ones placed on top of the larger ones below it.

So to achieve that, you would need to first gain a list of the different table rows, which can be most easily done by placing an id attribute on the table itself and using document.getElementsByTagName to get the rows.

After you have the rows, you need to get the first td value from each row, which can be done in a similar way.

So first, get the rows, then the sizes from those rows, then sort those rows based on the numeric value of the size from that row.

spoilt
—
2012-12-04T22:51:15Z —
#9

ok, the tables dont have IDs or classes though. can i get to the table another way like #container table?

thanks

paul_wilkins said:

The original table can remain, for a simple way to reorder the table is to loop through those difference sizes in numerical order, from biggest to smallest, and move each row to the top of the table. That way as you work your way from the largest ones down to the smallest ones, the table ends up having the smallest ones placed on top of the larger ones below it.

So to achieve that, you would need to first gain a list of the different table rows, which can be most easily done by placing an id attribute on the table itself and using document.getElementsByTagName to get the rows.

After you have the rows, you need to get the first td value from each row, which can be done in a similar way.

So first, get the rows, then the sizes from those rows, then sort those rows based on the numeric value of the size from that row.

Paul_Wilkins
—
2012-12-05T04:01:29Z —
#10

spoilt said:

ok, the tables dont have IDs or classes though. can i get to the table another way like #container table?

thanks

That's nowhere near as effective. Is this not your page that you are using the scripting with?Without a useful identifier on the table, the script will become more brittle and vulnerable to breaking when other future changes occur to the page.

spoilt
—
2012-12-05T04:23:55Z —
#11

It is my website. I have recently set up a company as a dropshipper - so I have access to all of the product data from the company who fulfil the orders.I have copied across all of their sizing tables

paul_wilkins said:

That's nowhere near as effective. Is this not your page that you are using the scripting with?Without a useful identifier on the table, the script will become more brittle and vulnerable to breaking when other future changes occur to the page.

Paul_Wilkins
—
2012-12-05T04:36:57Z —
#12

spoilt said:

It is my website. I have recently set up a company as a dropshipper - so I have access to all of the product data from the company who fulfil the orders.I have copied across all of their sizing tables

Okay then, it shouldn't be a problem then for you to add a suitable identifier to the table then, such as:

<table id="sizings">

spoilt
—
2012-12-05T04:45:58Z —
#13

I can do but the issue is I've got over 300 size charts so I was hoping to add some code just to the template page, rather than go into each product's description and edit them individually

paul_wilkins said:

Okay then, it shouldn't be a problem then for you to add a suitable identifier to the table then, such as:

`html

`

spoilt
—
2012-12-05T06:54:55Z —
#14

so although its not best practice, is it possible to use the container's id? how would this be written? and can i add / edit size names to your code, e.g. 'superdog' as well as 'super dog'.

then at least i can fix all the tables immediately, then go into each one individually later to fix them properly.

thanks!

Paul_Wilkins
—
2012-12-05T11:27:57Z —
#15

spoilt said:

so although its not best practice, is it possible to use the container's id? how would this be written?

If you want to restrict the table element to be from within the div that has a class of "container main-content" then that gets trickier when you're needing the code to remain compatable with older web browsers.

That is complex though, and only works if the class names don't change or get added to.

Instead of that, it is preferable to use the querySelector that most modern web browsers now support:

var table = document.querySelector('.container.main-content table');

You can still use querySelector in browsers that don't support that feature, such as IE7, but you will also want to use this queryselector polyfill to add that missing querySelector functionality to web browsers that need it.

Another alternative without an identifier, assuming that it's the first or only table on the web page, is to use getElementsByTagName to get the table itself:

var table = document.getElementsByTagName('table')[0];

but be wary of using the above, for it's guaranteed to break if any table appears on the web page before the one that you're wanting to target.

With an identifier it would as simple as using the getElementById method:

var table = document.getElementById('sizings');

That's why using a consistent identifier allows you to very easily target the sizings table from the script.

spoilt said:

and can i add / edit size names to your code, e.g. 'superdog' as well as 'super dog'.

You will see in the test code that I used before, that both of those situations result in the correct behaviour. It can handle superdog with and without spaces, and with a number suffix too.

spoilt
—
2012-12-05T12:18:01Z —
#16

paul you've been an incredible help!i feel like i owe you something!

the table is the only one on the page, so to ensure the code works in IE7 do you recommend using this

var table = document.getElementsByTagName('table')[0];

so if i just add all of the code from before, then ... aaagh sorry i feel like an idiot but can you just help me connect the two so i can get it all working. otherwise i know i would stay up all night trying to figure out the last bit (i know its as good as complete in your mind lol)

thanks again!!

Paul_Wilkins
—
2012-12-05T12:23:50Z —
#17

spoilt said:

so if i just add all of the code from before, then ... aaagh sorry i feel like an idiot but can you just help me connect the two so i can get it all working. otherwise i know i would stay up all night trying to figure out the last bit (i know its as good as complete in your mind lol)

There's a hell of a lot more that's to be done yet in terms of getting the rows, gathering up the sizes, working out what order they should be in, and actually ordering the rows of the table.If you don't know how to write code and want someone else to do the work for you instead, then my time that I volunteer here is too limited to allow me to do all of that work for you.

If on the other hand you can put together an attempt at solving those problems - when you have any technical issues with them we can help to educate you about such things.

spoilt
—
2012-12-05T12:28:00Z —
#18

as i said im a js newbie, especially as it gets more complicated.not asking anyone to do all the work for me... i'll give it the best shot i can

Paul_Wilkins
—
2012-12-05T23:49:17Z —
#19

Actually, I should be able to take the time to go through this, due to the rest of it being made fairly simple by combining some built-in techniques.

Starting with the table content, we want to get the tbody rows and sort them.

There are a number of different ways to sort things but in this case, given that they are an HTML collection, the easiest way to sort them is to create an array from those HTML elements by using the Array's slice method to convert non-array items in to an array. That way the array sort method can be used to sort the rows in that array.

Then, we can then loop through the sorted array and move each row to the bottom. The sortRows function that results in the rows all being nicely sorted, is:

The sort method that arrays have is what we'll use to sort the rows, so that compareSizes function that we give to the sort method accepts two items being compared, and returns either -1/0/+1 or false/true to indicate which one is larger than the other.

hi paul - thanks so much for doing this, it all looks great once its come together

ive just copied and pasted the jsfiddle code onto a page that i can test it on, then make it match the IDs and classes on my page, and tweak anything, etc, but surprisingly even a straight copy / paste doesn't work.

ive checked there are no ID or class conflicts (just had to change the class 'main-content'), but not sure why its running on jsfiddle but not on my page. perhaps a jquery conflict i dont know.. really sorry i cant figure it out - i have been trying and ill keep looking. here is the link http://site-1111.myshopify.com/pages/new