Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
How to display random content from external .html file?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Justine  
View profile  
 More options Oct 30 2008, 2:37 pm
From: Justine <m.se...@gmail.com>
Date: Thu, 30 Oct 2008 19:37:56 +0100
Local: Thurs, Oct 30 2008 2:37 pm
Subject: How to display random content from external .html file?
Hello.

I'm relatively new to jQuery, never used JavaScript in my websites  
before but with jQuery it seems so incredibly easy that I've decided  
to learn it finally. I mostly use plugins, sometimes slightly modyfing  
the code etc but getting better every day I think :)

Anyway, I have a problem with writing a function that will make this  
effect possible: I want to display random content from external .html  
file in my 'main' file.

Lets say I have an index.html file with <div id="quotes"> and a  
texts.html file with <div id="texts"> inside which I have few <p  
class="text"> elements. What I want to do is simply randomly display  
one of these <p> elements inside the "quotes" div in my index file. I  
*think* :) it's possible but being a newbie I can't really figure out  
how to achieve this effect. Tried to find a plugin but only found  
stuff to display random content that's inside my index file.

Any help would be appreciated, thanks in advance!

--
Justine


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ricardobeat  
View profile  
 More options Oct 30 2008, 5:38 pm
From: ricardobeat <ricardob...@gmail.com>
Date: Thu, 30 Oct 2008 14:38:50 -0700 (PDT)
Local: Thurs, Oct 30 2008 5:38 pm
Subject: Re: How to display random content from external .html file?
This should work:

1. find out how many quotes you have (obj.length)
2. get a random number
3. hide all, and then select and show one of them using eq(index) -
http://docs.jquery.com/Traversing

$('#quotesContainer').load('quotes.htm',function(){
      var $quotes = $(this).find('p.text');
      var n = $quotes.length;
      var random = Math.floor( Math.random()*n );
      $quotes.hide().eq(random).fadeIn();

});

- ricardo

On Oct 30, 4:37 pm, Justine <m.se...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Justine  
View profile  
 More options Oct 31 2008, 3:51 am
From: Justine <m.se...@gmail.com>
Date: Fri, 31 Oct 2008 08:51:45 +0100
Local: Fri, Oct 31 2008 3:51 am
Subject: Re: [jQuery] Re: How to display random content from external .html file?
On Oct 30, 2008, at 10:38 PM, ricardobeat wrote:

> This should work:
> (...)

> $('#quotesContainer').load('quotes.htm',function(){
>      var $quotes = $(this).find('p.text');
>      var n = $quotes.length;
>      var random = Math.floor( Math.random()*n );
>      $quotes.hide().eq(random).fadeIn();
> });

Works like a charm! Exactly what I needed. Thanks a lot!

While we're at it... what should I change/add to make it display one  
quote for, say, 30 secs then display another one, without needing to  
reload a page?

--
Justine


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google