jQuery Comment Preview
I released a MooTools comment preview script yesterday and got numerous requests for a jQuery version. Ask and you shall receive! I'll use the exact same CSS and HTML as yesterday.
The XHTML
<div id="live-preview-form" class="lp-block"> <p> <strong>Your Name:</strong><br /> <input type="text" name="name" id="name" value="" class="input" /><br /><br /> <strong>Your Email:</strong><br /> <input type="text" name="email" id="email" value="" class="input" /><br /><br /> <strong>Your Website:</strong><br /> <input type="text" name="website" id="website" value="" class="input" /><br /><br /> <strong>Your Comment:</strong><br /> <textarea name="comment" id="comment" class="input" rows="10"></textarea> </p> </div> <div id="live-preview-display" class="lp-block"> <div id="lp-avatar"></div> <div id="lp-name"></div> <div id="lp-comment"></div> </div>
The CSS
.lp-block { width:400px; float:left; } .lp-block input, .lp-block textarea { width:90%; } #live-preview-display { background:#eee; padding:10px; margin-left:50px; margin-top:20px; } #lp-name { font-weight:bold; } #lp-avatar { float:right; margin:0 0 20px 20px; } #lp-comment { padding-top:10px; font-style:italic; line-height:19px; }
The jQuery JavaScript
$(document).ready(function() { var commentNode = $('#lp-comment'), nameNode = $('#lp-name'), name = $('#name'), website = $('#website'); //comment...easy $('#live-preview-form input, #live-preview-form textarea').bind('blur keyup',function() { //comment commentNode.text($('#comment').val()); commentNode.html($('#lp-comment').html().replace(/\n/g,'<br />')); //name & websites if($('#name').val()) { if(website.val() && /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{2}/.test(website.val())) { nameNode.html('<a href="' + website.val() + '">' + name.val() + '</a> says:'); } else { nameNode.text(name.val() + ' says:'); } } //gravatar if($('#email').val() && /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test($('#email').val())) { var md5Email = MD5($('#email').val()); $('#lp-avatar').html('<img src="http://www.gravatar.com/avatar.php?gravatar_id=' + md5Email + '&size=80&rating=G&default=http%3A%2F%2Fdavidwalsh.name%2Fwp-content%2Fthemes%2Fdavid-walsh%2Fgraphics%2Fcom.jpg" alt="' + $('#lp-name').val() + '" />'); } }); });
On the keypress and blur events, we validate and format the commenter's name, avatar, website, and comments as necessary. As with yesterday's MooTools post, the MD5 function can be found here.
Too easy!
Brilliant, thank you very much :)
I would perhaps use the keyup rather than keypress event, so that the final character you type appears without you having to move your focus away from the textarea.
Otherwise, really nice :)
I agree with the keyUp comment, but still very nice!
Changed to keyup. I didn’t notice at first — i must type too fast.
You are the best…!
Noticed a bit of jumping in FF, try throwing a
min-height
on your display div to fix that (won’t work in IE 6 of course but eh.) Great job though, post via AJAX for a next step?I just implimented this on my site. At first I was including the md5 function on an external file and firebug was saying that the function was undefined, however when I put it straight on the document it worked fine.
The filename was definatly correct. Would there be any other reason for this?
@Ben: Hard to say without seeing the page/code.
Thanks for taking the time to port this to jQuery David, you’re the beez neez.
holy crap that’s cool, I really need to start learning javascript.
It’s nice bro, but just one thing. It would be nice to use some line-breaking function, so when the word is too long it goes out of the box :) U can use this JS Wordwrap .
very very nice! what a nice feature for the visitors. thank you for sharing!
Thank’s, very nice!
oh my god this is so sick!
Man I loved that!!!
Brilliantly awesome dude!!!
Is it possible to make it process HTML tags? For example TEXT would make “TEXT” appear bold?
@James Wong:
Try changing
To
@Ben: Like magic! works like a charm :D ty
i noticed that the gravatar does not work it always shows the default gravatar
any sugestions on how to fix that (i thought it might be it needs to pass the email in lowercase){i dont know jquery much so i dont know if its doing that}
i am currently trying to convert the md5 and as meny things as i can to php so can save the data in mysql and use on my site. but im a bit lost and its driving me nuts
i did get php to work with gravatar on my site.
any help would be grate to convert this to a jquery/php with save features
i did find a nifty mysql jquery routine
http://www.phpdeveloper.org/news/12003
that has the mysql routines to use for coments
so any ideas on how to incorp all this and to fix the bug of only displaying the default gravatar?
thank you in advance for any code or help you may provide
this is the php is has the problem of not displaying the default gravatar
go figure
<?php
$email = "dragonsworkshop@yahoo.com";
$default = "http//:davidwalsh.name/wp-content/themes/david-walsh/graphics/com.jpg";
$size = 50;
//You can construct your gravatar url with the following php code:
$grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=".md5( strtolower($email) ).
"&default=".urlencode($default).
"&size=".$size;
echo '’;
?>
echo ‘IMG SRC=”‘.$grav_url.'” ALT=”?” BORDER=0’;
add a less then and greater then signs for image (didnt print in last post thought it was html)
this now works fixed the default image php code
had to split the code(wouldn’t let me post thought i was spam)
$grav_url = “http://www.gravatar.com/avatar.php?gravatar_id=”.md5( strtolower($email) ).
“&d=”.$default.//urlencode($default).
//”&d=monsterid”.
“&size=”.$size;
if (validate_gravatar($email))
{
echo ‘IMG SRC=”http://’.$grav_url.'” ALT=”?” BORDER=0′;
}
else
{
echo ‘no gravatar found’;
echo ‘IMG SRC=”‘.$default.'” ALT=”?” BORDER=0’;
echo “org gravatar”;
echo ‘IMG SRC=”‘.$grav_url.'” ALT=”?” BORDER=0’;
}
// replace less then and greater then signs for image as needed
?>
[code]
Thanx I really needed a comment box you’ve saved my life
Here’s my version, dating from the year 2005
(onkeyup="ReloadTextDiv()" on the textarea)
– short and sweet:Hello,
More than 15 characters (And stop at 255 characters) long when I want to run. But it did not work. Please help me,
Sincerely,
Samet ARAS.
@Ben: great
@Rob Simpkins: great great great great great great
I have a form with three selection menus. but every time i run this script I’m have trouble getting only one selection menu to how it’s change in it’s corresponding #sum_… div
If i change the value of the #game_time selection menu, the other two selection menus also change their value,
How can i stop this?
Here is my current script
Thanks!!!
I have a form with three selection menus. but every time i run this script I’m have trouble getting only one selection menu to how it’s change in it’s corresponding #sum_… div
If i change the value of the #game_time selection menu, the other two selection menus also change their value,
How can i stop this?
Here is my current script
Thanks!!!
Hi all, am new to JQuery & i need to add comments/Post on my website am develpoing. please i can any one help me with a demo or link to get a download?
Thanks
@chris: u re right man
Just what I was looking for – thank you for sharing.
.d.
Is nice when is all script untul finish, and how the next script that show the comments?
Regard
Wiyono
why wont this text allow you to style links and bold and italic font in the preview? is there an easy way to do that?
Sorry guy for my poor english…
, but you do remove the “_” before and after tag ()
I’m a newb to programming/coding and this is probably a stupid question to you guys… where do I paste the jquery code?
Thanks for the help
test
Hi david, Can you say me that how to make a comment box for my website which the people will use to post comments on my website.
The same like which you have,
Please Reply Soon….
Thanks,
Adil
hey..good post dud…but this download link is not..can you add with zip file it..i think very important all of…thank u……….
Nadun
comments adasdasdasdasdasdasdasd
I adore a couple of the articles which were written, and especially the comments posted! I will definitely be returning! The content of your post is very fresh, very worth reading
Fantastic function! That’s the kind of data which are designed to end up being discussed round the internet. Shame about the look for not really placement this particular submit top! Seriously more than as well as talk to my personal website. Many thanks =)
I really like your website! It’s perfectly created; I really like exactly what you’ve reached state. However you may might a bit more when it comes to content material therefore individuals might interact with this much better. cheap tory burch outlet online welcome to toryburchoutletsshop.com! Many thanks.
Tory burch outlet online was lucky to see your website! your website can reflect intellectual charm. Tory burch outlet online will as always watching you website. Many thanks with regard to discussing.
Read a son of the building Lord, my mood is to be calm. As Lao tze have cloud: great music and sound, elephant invisible. I finally understand what I lack now is what, exactly the building Lord that kind of persist to pursue to the truth and the building Lord that kind of to ideal hard practice produce of decorous feeling.
Just wanted to grant you a shout from the valley of the sun, great information. Much appreciated.
I would like to watch your article in my entire life. this article conceives outline novel, the topic has the mental strategy only, the paragraph is clear , fall the rise and fall, the main line is clear, fascinating, mild the extraordinary literature from bottom inside, is witty it may be said, a classic, is a model that my generation should the study.
http://www.drbeatsbydre.com/
I’m intersting about your content .keep up the good work.
Absolutely Fantastic, thank you!
How would add this to blogger , i need to change bloggers format, i dont no how?
Please tell me , what is a sumbit button for this script ??
i agree with you! and your suggestion is all good! i have see a lot of website, and your blog is the better one! yes!!!thanks for your share.
The post is written in very a good manner and it entails much useful information for me. I am happy to find your distinguished way of writing the post.
The post is written in very a good manner and it entails much useful information for me. I am happy to find your distinguished way of writing the post.
Love the newest look. I enjoyed the content. Thanks for the great work.
michael kors handbags:http://www.michaelkorsshandbagsoutlet.com/
Very informative post thanks for share this with us i highly appreciate you for this information thanks once again for sharing information like this!
thanks for sharing!! pretty cool and easy to understand.
very very nice! Thank you <code
Lorem ipsum dolor siit amet, consectetur adipkscing elit.
Just desire to say your article is as surprising. The clarity in your post is simply
excellent and i can assume you’re an expert on this subject.
Fine with your permission allow me to grab your RSS feed to keep up to
date with forthcoming post. Thanks a million and please keep up the enjoyable work.
Great article.
If you desire to take a great deal from this paragraph then you have to apply these methods to your won
web site.
Pretty nice post. I just stumbled upon your blog and wanted
to say that I’ve truly enjoyed browsing your blog posts.
In any case I’ll be subscribing to your rss feed and I hope you write
again soon!
Quality posts is the important to be a focus for the visitors to visit the web
page, that’s what this website is providing.
This design is steller! You definitely know how to keep a reader entertained.
Between your wit and your videos, I was almost moved to start my own blog
(well, almost…HaHa!) Excellent job. I really loved what you had to say, and more than that,
how you presented it. Too cool!