Max Glenister

Web Dev • 1 min read Plain text

Building JavaScript arrays from HTML using regular expressions

Bit of idea ping-pong with Tomas this evening, to provide a solution to something he is working on. He had some text in a table cell that needed to be split and put into an array, separating each line into two parts.

This is the snippet we came up with, to build a flat array

$(function() {
    var array = $("p.src").html()
        .split(/([0-9]{1})\s([A-Za-z\s]+\s*[0-9°º]*)/);
    var position = 0;

    while (position != -1) {
        array.splice(position, 1);
        position = array.indexOf('<br>');
    }

    $.each(array, function(index, value) {
        $("#array").append(index + " => " + value + "<br />");
    });
});

Nice concise bit of Javascript, after iterating through a bunch of (over-complicated array rebuilding on my part) versions.. :D

  • Web Dev · shares #jquery
    Using jQuery to create a custom thumbnail for an embedded video

    Recently Jenny asked me for a way to embed a YouTube video with a custom thumbnail that would play the video when clicked.

    May 2012 • 1 min read
  • Web Dev · shares #jquery
    A simple solution for a jQuery animation loop memory leak

    I’m probably stating something very obvious to everybody, but this is something that caused me a couple of hours of confusion. When creating a new slightly-fancier page to be displayed on omgmog.net I had a problem in that after a certain amount of time the browser would become unresponsive (or in Google Chrome the page would die - but I love how Google Chrome handles this). The problem I soon realised was in my animation loop. I’ve got a couple of lines of jQuery that move some elements on the page around randomly, to give a floating effect. The original code was as follows: $("h1 span").not("#h1p3").everyTime(0,function(){ $(this).animate({'margin-left':rand_no(),'margin-top':rand_no()}); }); It turns out the problem with this code is that the animation was looping before it had completed, so the browser presumably is stacking up all of these $().animate(); functions, which in turn caused the browser to become unresponsive. To remedy this, I specified a time for the $().animation(); function, and set the $().everyTime(); timeout to slightly longer than this: $("h1 span").not("#h1p3").everyTime(500,function(){ $(this).animate({'margin-left':rand_no(),'margin-top':rand_no()},490); }); With these small changes in place, it seems my problem is solved! I don’t have any “lessons learned” speech to close the post with, it’s just something that I found interesting :)

    December 2010 • 2 min read
  • Web Dev · shares #jquery
    Using jQuery to supply and clear default input values

    This is a repost of a solution I provided over at the smashing magazine forums. I’m posting it here as it’s a pretty useful solution in itself. The original question is as follows: I’m building a new website and on this one page I need to use a huge form, instead of using labels I want to enter the “label” text in the value attribute and then use JS to clear the field on mouseclick. The problem is that every field needs to be filled out. I don’t want users hitting the submit button with some of the standard values still in place (easy to miss certain stuff) because this will mess things up. I can ofcourse check for empty fields but to do this I need to somehow check if all the fields have been changed on form submit. So I whipped up a quick implementation of how I would do it using jQuery.

    January 2010 • 2 min read

Discussion

No interactions yet

Join the conversation

Reply from your own site with a Webmention (how?).

Webmentions last fetched 15 November 2026, 16:57