/    Sign up×
Community /Pin to ProfileBookmark

Help setting font programattically

I have the following function:

function randq()
{
while(document.getElementById(IDOfParagraph).childNodes.length>0)
{
document.getElementById(IDOfParagraph).removeChild( document.getElementById(IDOfParagraph).firstChild );
}

var q = quotations[Math.round(Math.random()*(quotations.length-1))];
currentParagraph = document.getElementById(IDOfParagraph);

currentParagraph.style.color = ‘rgb(0,195,166)’;

currentParagraph.appendChild( document.createTextNode( q ) );
}

Quite simply, I’d like to set the currentParagraph object’s font to Century Gothic, size 2.

Also, sometimes the resulting currentParagraph has the string ‘undefined’ in it. Is the way i’m using math.random correct? Seems like ‘undefined’ is the result of an out of bounds array index.

Thanks,
Barry

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@PittimannFeb 24.2005 — Hi!

For color and size:

currentParagraph.style.fontFamily='Century Gothic';

currentParagraph.style.fontSize='10pt';//approx. equivalent to deprecated <font size="2">

Concerning the 'undefined': how does your array look like? In Lavalamp's original version, he had all text nodes inside the relevant <p> tags automatically be added to corresponding arrays.

Could you show us the code you are currently using?

Cheers - Pit
Copy linkTweet thisAlerts:
@banderbergauthorFeb 24.2005 — Yeah I didn't want the quotes right out in the html like that..

it's just an array like so

quotations[0] = ""hello" - Anonymous"

.

.

quotations[n] = ""hello again" - Anonymous"



of course the .. [n] notation isn't actually in my code.. it's a standard array from 0 to n.

I did cut and paste a lot of the quotes off the web.. I'm wondering if one of them has some embedded character that isn't part of the standard ascii character set and that upsets the javascript run time.
Copy linkTweet thisAlerts:
@PittimannFeb 24.2005 — Hi![i]Originally posted by banderberg [/i]

[B]I'm wondering if one of them has some embedded character that isn't part of the standard ascii character set and that upsets the javascript run time. [/B][/QUOTE]
That would rather cause a js error to show up, but not write 'undefined' to your paragraph. Well - you say, your array looks like that:

var quotations=new Array();

quotations[0] = ""hello_0" - Anonymous"

quotations[1] = ""hello_1" - Anonymous"

quotations[2] = ""hello_2" - Anonymous"

quotations[3] = ""hello_3" - Anonymous"

...

quotations[n] = ""hello_n" - Anonymous"

I am quite sure that you are simply missing one or more array elements in the consecutive series of numbers from 0 to n. Example: n is 753, but entry 347 is not present (because you left it out or mistyped the number); due to that you will have 'undefined' once in a while.

Cheers - Pit
Copy linkTweet thisAlerts:
@banderbergauthorFeb 24.2005 — Most impressive. I can't believe I didn't see that. Sure enough I skipped a number in the series.

How about answering yet another question? ?

I'd like to insert a line break in my quotes after they reach a certain length.

I've tried putting <br> in the string, but it shows up as <br> on the webpage.

Maybe I'll have to do some string parsing of some kind.. the problem is that some of these strings are longer than the cell I'm putting them in but they line feed at awkward places that make the quote look goofy, such as the author's first name on one line and last name on the next.

Again, thanks for all your help.
Copy linkTweet thisAlerts:
@PittimannFeb 24.2005 — Hi!

You're welcome. Concerning the br's: document.createTextNode doesn't accept HTML tags, only pure text. You could switch from the DOM method to using innerHTML but if you want to stay with the DOM stuff and only avoid line breaks in such places like you described, you could insert a non-breaking space between words not to be separated instead of a normal space.

The 'normal' non-breaking space (&amp;nbsp? wouldn't work here - just like the tags. The utf code for it is 'u00A0' (without the single quotes) and it would do the job.

Cheers - Pit
×

Success!

Help @banderberg spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...