Welcome Guest!
Please login
If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:
- Receive Fast Advice
- Learn Programming Languages
- Get Professional Website Reviews
- Quick Troubleshooting Assistance
Realtime Preview Updating From Textarea
This is a discussion on Realtime Preview Updating From Textarea, within the Javascript section. This forum and the thread "Realtime Preview Updating From Textarea" are both part of the Frameworks category.
![]() ![]() |
Sep 9 2008, 07:40 PM
Post
#1
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 146 Joined: 3-July 08 |
I have a textarea that as I type into it, I would like another div on the page to update with the stuff being put into the textarea. I want to do this for a few fields as well because its an email response kind of program that will have to, from, subject, body etc.
Can somebody teach me how to fish? |
|
|
Sep 9 2008, 07:58 PM
Post
#2
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
You're a jQuery convert right? I'll use that as an example:
CODE <div id="preview"> </div> <textarea id="writing"> </textarea> <script type="text/javascript"> $(document).ready(function() { $('#writing').bind('keyup change blur focus', function() { $('#preview').html($(this).val()); }); }); </script> It's a bit bloated (6 lines) but should suffice... -------------------- |
|
|
Sep 10 2008, 03:43 AM
Post
#3
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 146 Joined: 3-July 08 |
its unbelievable... 6 lines. It works beautifully.
I am totally jquery converted. I just want to use it more and more its such a great framework. I have used many others but none come close to having such a simple bunch of building blocks that can all work together in ways I have yet to see in others! by any chance do you know if that var can be displayed with js the way nl2br() works in php? So like a line return in the textarea equates to a line break when it spews it out in the preview pane? |
|
|
Sep 10 2008, 05:31 AM
Post
#4
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
$('#preview').html($(this).val().replace([\n], '<br />'));
-------------------- |
|
|
Sep 10 2008, 05:40 AM
Post
#5
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 146 Joined: 3-July 08 |
$('#preview').html($(this).val().replace([\n], '<br />')); Super cool. I tried that... well... a variation of that because it appears it doesn't like thet [\n] so i just put a '\n' and that worked... but it only partially worked like it only puts in 1 br and you can't do any more than that.. for example see the screen shot for the textarea and the preview
Picture_9.png ( 66.09K )
Number of downloads: 5 |
|
|
Sep 10 2008, 06:34 AM
Post
#6
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
Sorry, I forgot the slashes
$('#preview').html($(this).val().replace(/[\n]/g, '<br />')); -------------------- |
|
|
Sep 10 2008, 07:43 AM
Post
#7
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 146 Joined: 3-July 08 |
What does that stuff do?
Like what does '/[\n]/g' actually mean and how did you learn it? |
|
|
Sep 10 2008, 08:23 AM
Post
#8
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
It's PCRE (Pearl Compatible Regular Expressions) and javascript actually has a pretty powerful inclusion of them.
That one from above can be broken down / open regex [\n] - match against a newline character.. You don't need the square brackets but I pop 'em in anyway. / close regex g tell the regex to perform the pattern search on the entire string A good place to start learning is on http://regularexpressions.info -------------------- |
|
|
Sep 10 2008, 07:37 PM
Post
#9
|
|
![]() Squeeze Machine ![]() ![]() ![]() ![]() ![]() Posts: 733 Joined: 13-February 08 From: Borneo |
Cool stuff from you again mate!
I am gonna bookmarked this post It might come's handy in the future... -------------------- |
|
|
Sep 10 2008, 09:05 PM
Post
#10
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 146 Joined: 3-July 08 |
Very awesome.
I noticed the reg expression isn't in quotes. Also I thought that was a mistake you made... silly me. It doesn't work with quotes! Thanks heaps Rakuli. |
|
|
Sep 10 2008, 09:10 PM
Post
#11
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
Yeah, when you use /regex/ -- javascript will create a new RegExp object and doesn't need quotes.
If you want to use quotes (or include variables in your regex) you need to manually create the object. var regex = new RegExp('/regex' + variable + '/); string = string.replace(regexp, ''); -------------------- |
|
|
Oct 8 2008, 11:46 AM
Post
#12
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 146 Joined: 3-July 08 |
I have been using this script for a couple months now and our backend has become a lot more interactive. People can drop messages to one another... when users do something a little message drops in the window like "Bobby has connected a new event" and "Ron has purchased a ticket" and "James invited such and such to an event and said 'This looks great'".
I've seen people logging into our backend much more often now to keep on the pulse of everything. Its pretty neat seeing the activity log of our business close to realtime. I have found though a slight technical issue using this script and it occurs when say we call a fairly large data populate php file via an ajax window. It appears this timer will keep trying to refresh when the browser window is busy perhaps loading the next page... sometimes collecting a queue of requests that ultimately can time out-under strenuous circumstances. We've been wondering if there might be a simple addition to this function that basically puts this update on pause whenever there is other activity going on. We have found the error pops up less when we've made the timer longer and therefor decreased the tempo of the auto update... lightening the load etc. Have I missed the mark on this? Firefox reports the error like so: QUOTE A script on this page may be busy, or it may have stopped responding. You can stop the script now, or you can continue to see if the script will complete. Script: https://www.triumphantevents.co.uk/js/jquery.js:11 Any ideas on a solution? The code being... CODE function callMeOften()
{ $j.ajax({ method: 'get', url : 'myupdatingfile.php', cache : false, dataType : 'text', success: function (text) { $j('#updateme').html(text); } }); } var holdTheInterval = setInterval(callMeOften, 13000); This post has been edited by cosmicbdog: Oct 8 2008, 11:47 AM |
|
|
Oct 8 2008, 04:50 PM
Post
#13
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
You could change it so that it doesn't call the function unless the previous request has finished:
CODE var holdTheInterval =false; function callMeOften() { $j.ajax({ method: 'get', url : 'myupdatingfile.php', cache : false, dataType : 'text', success: function (text) { $j('#updateme').html(text); holdTheInterval = setTimeout(callMeOften, 1300); }, failure: function(){ setTimeout(callMeOften, 1300); } }); } So now the ajax won't call until the previous request has finished, whether that be success or failure. -------------------- |
|
|
Oct 8 2008, 08:08 PM
Post
#14
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 146 Joined: 3-July 08 |
You could change it so that it doesn't call the function unless the previous request has finished: CODE var holdTheInterval =false; function callMeOften() { $j.ajax({ method: 'get', url : 'myupdatingfile.php', cache : false, dataType : 'text', success: function (text) { $j('#updateme').html(text); holdTheInterval = setTimeout(callMeOften, 1300); }, failure: function(){ setTimeout(callMeOften, 1300); } }); } So now the ajax won't call until the previous request has finished, whether that be success or failure. I tried the above example under the most strenuous circumstances and guess what? No error. High five from qld man. |
|
|
Oct 8 2008, 09:34 PM
Post
#15
|
|
![]() Squeeze Machine ![]() Posts: 508 Joined: 7-October 08 From: Australia |
Nice one, Rakuli!
Also, I know the conversation has moved on a bit from regex, but seeing as how you've bookmarked this thread, if you're looking for a way to trial-and-error your regular expressions without editing your javascript, saving, refreshing, etc. etc. you can use http://regextester.com/, it's fantastic for testing regex with both search and find/replace -------------------- The more you visit, the more I'll post: http://japheththomson.com/
|
|
|
Oct 9 2008, 11:19 PM
Post
#16
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 146 Joined: 3-July 08 |
It appears that as I was pouring a celebratory drink... one of our users wrote me a bug report saying the same issue was still occuring. doh.
Back to the drawing board |
|
|
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
3 | sodapop | 397 | 25th May 2008 - 08:01 AM Last post by: sodapop |
|||
![]() |
0 | rubyshack | 193 | 30th October 2008 - 09:49 PM Last post by: rubyshack |
|||
![]() |
3 | Simon88 | 212 | 21st November 2008 - 11:21 AM Last post by: Simon88 |
|||






Sep 9 2008, 07:40 PM













