s1yman Posted August 24, 2008 Share Posted August 24, 2008 Hi, Does anyone know how to insert the value of a prompt into a text area? Thanks. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 25, 2008 Share Posted August 25, 2008 Well, there are many ways this could be implemented and the optimal way to implement will depend on your process. But, here is one example: <html> <head> <script type="text/javascript"> function trigger_prompt(fieldID) { var replyTxt = prompt("What year were you born?", "") document.getElementById(fieldID).value = replyTxt; } </script> </head> <body> Birth year: <input type="text" id="birthYear" /><br> <button onclick="trigger_prompt('birthYear');">Trigger Prompt</button> </body> </html> Quote Link to comment Share on other sites More sharing options...
s1yman Posted August 25, 2008 Author Share Posted August 25, 2008 Hi, Thanks for the quick response. I am trying to recode a small part of this RTE I downloaded the orginal code is; function insertLink(rte) { //function to insert link var szURL = prompt("Enter a URL:", ""); try { //ignore error for blank urls rteCommand(rte, "Unlink", null); rteCommand(rte, "CreateLink", szURL); } catch (e) { //do nothing } } I want it to also say 'Enter link text' so it is more simple for the users. I got the prompt working but cannot work out how to actually output the text. Any ideas?; function insertLink(rte) { //function to insert link var linkt = prompt("Enter link text",""); var szURL = prompt("Enter a URL:", ""); try { //ignore error for blank urls rteCommand(rte, "Unlink", null); document.getElementById(rte).value = linkt; rteCommand(rte, "CreateLink", szURL); } catch (e) { //do nothing } } Thanks for the help. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 25, 2008 Share Posted August 25, 2008 I'm not sure what to tell you. The function rteCommand is someting outside of that function. That is most likely what is causing your problem. But, if I comment those lines out the function works fine. You need to look at your function for rteCommand() for the problem. <html> <head> <script type="text/javascript"> function insertLink(rte) { //function to insert link var linkt = prompt("Enter link text",""); var szURL = prompt("Enter a URL:", ""); try { //ignore error for blank urls // rteCommand(rte, "Unlink", null); document.getElementById(rte).value = linkt; // rteCommand(rte, "CreateLink", szURL); } catch (e) { //do nothing } } </script> </head> <body> Link output field: <input type="text" id="linkOutput" /> <br /><br /> <button onclick="insertLink('linkOutput');">Click for Prompt</button> </body> </html> Quote Link to comment Share on other sites More sharing options...
s1yman Posted August 25, 2008 Author Share Posted August 25, 2008 Is that the one? function rteCommand(rte, command, option) { //function to perform command var oRTE; if (document.all) { oRTE = frames[rte]; } else { oRTE = document.getElementById(rte).contentWindow; } try { oRTE.focus(); oRTE.document.execCommand(command, false, option); oRTE.focus(); } catch (e) { // alert(e); // setTimeout("rteCommand('" + rte + "', '" + command + "', '" + option + "');", 10); } } I'm bit useless with javascript, I appreciate your help! Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted August 25, 2008 Share Posted August 25, 2008 Try this: <html> <head> <script type="text/javascript"> function insertLink(rte) { var linkt = prompt("Enter link text",""); if (linkt == "") { alert("No text given."); return; } var szURL = prompt("Enter a URL:", ""); if (szURL == "" || !/^(http:\/\/)?(www\.)?[a-z0-9_-]+\.[a-z]{2,4}\..*$/i.test(szURL)) { alert("Not valid URL"); return; } document.getElementById(rte).value = linkt; } </script> </head> <body> Link output field: <input type="text" id="linkOutput" /> <br /><br /> <button onclick="insertLink('linkOutput');">Click for Prompt</button> </body> </html> Quote Link to comment Share on other sites More sharing options...
s1yman Posted August 25, 2008 Author Share Posted August 25, 2008 Thanks mate .. the pop-ups work, but there doesn't seem to be any output still ??? any ideas? I put the function exactly like you suggested Quote Link to comment Share on other sites More sharing options...
s1yman Posted August 25, 2008 Author Share Posted August 25, 2008 The text area is using this code; <html><form enctype="multipart/form-data" id="form" name="form" method="post" action="formpro.php" onsubmit="return submitForm();"><script language="JavaScript" type="text/javascript"> <!-- function submitForm() { //make sure hidden and iframe values are in sync before submitting form //updateRTE("rte1"); //use this when syncing only 1 rich text editor ("rtel" is name of editor) updateRTEs(); //uncomment and call this line instead if there are multiple rich text editors inside the form alert("Submitted value: "+document.myform.rte1.value) //alert submitted value return true; //Set to false to disable form submission, for easy debugging. } //Usage: initRTE(imagesPath, includesPath, cssFile) initRTE("images/", "", ""); //--> </script> <div><script language="JavaScript" type="text/javascript"> <!--//Usage: writeRichText(fieldname, html, width, height, buttons, readOnly) writeRichText('one', '', 400, 200, true, false);//--></script></div> And the JS links to; document.writeln(' <td><img class="rteImage" src="' + imagesPath + 'hyperlink.gif" width="25" height="24" alt="Insert Link" title="Insert Link" onClick="insertLink(\'' + rte + '\')"></td>'); Does that make sense? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted August 25, 2008 Share Posted August 25, 2008 Sorry, my bad. I made a little mistake. This should work: <html> <head> <script type="text/javascript"> function insertLink(rte) { var linkt = prompt("Enter link text",""); if (linkt == "" || linkt === null) { alert("No text given."); return; } var szURL = prompt("Enter a URL:", ""); if (szURL == "" || szURL === null || !/^(http:\/\/)?(www\.)?[a-z0-9._-]+\.[a-z]{2,4}\.?.*$/i.test(szURL)) { alert("Not valid URL"); return; } document.getElementById(rte).innerHTML = "<a href='" + szURL + "'>" + linkt + "</a>"; } </script> </head> <body> Link output field: <span id="linkOutput"></span> <br /><br /> <button onclick="insertLink('linkOutput');">Click for Prompt</button> </body> </html> Quote Link to comment Share on other sites More sharing options...
s1yman Posted August 25, 2008 Author Share Posted August 25, 2008 Still doesn't seem to work mate? :-\ Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted August 25, 2008 Share Posted August 25, 2008 Still doesn't seem to work mate? :-\ Works for me. I tested it. Can you provide the URL to the site you have that code? Quote Link to comment Share on other sites More sharing options...
s1yman Posted August 25, 2008 Author Share Posted August 25, 2008 Still doesn't seem to work mate? :-\ Works for me. I tested it. Can you provide the URL to the site you have that code? http://project.computersmk.co.uk/test/editor/form.html I appreciate the help btw. Quote Link to comment Share on other sites More sharing options...
s1yman Posted August 25, 2008 Author Share Posted August 25, 2008 http://project.computersmk.co.uk/test/editor/form.html The javascript is in the same directory, named richtext.js Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted August 25, 2008 Share Posted August 25, 2008 Oh, no wonder. You should've provided the site in the first post. I haven't fully studied the code, but give this a go: function insertLink(rte) { var linkt = prompt("Enter link text",""); if (linkt == "" || linkt === null) { alert("No text given."); return; } var szURL = prompt("Enter a URL:", ""); if (szURL == "" || szURL === null || !/^(http:\/\/)?(www\.)?[a-z0-9._-]+\.[a-z]{2,4}\.?.*$/i.test(szURL)) { alert("Not valid URL"); return; } var oLink = "<a href='" + szURL + "'>" + linkt + "</a>"; rteCommand(rte, "CreateLink", oLink); } Quote Link to comment Share on other sites More sharing options...
s1yman Posted August 25, 2008 Author Share Posted August 25, 2008 oh, sorry mate. That edit you have just posted creates a link on text that you have highlighted, but doesn't input the text "linkt" Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted August 25, 2008 Share Posted August 25, 2008 oh, sorry mate. That edit you have just posted creates a link on text that you have highlighted, but doesn't input the text "linkt" I'm sorry, but I don't follow. Can you show me what it does show? By the way "linkt" is a variable. Quote Link to comment Share on other sites More sharing options...
s1yman Posted August 26, 2008 Author Share Posted August 26, 2008 Ok, what I mean is that you click on the 'insert link' and the prompts appear fine. However no text is inserted into the box. However, if you have text highlighted (scrolled over) at the time you select 'insert link' then it will create a link. Hope that makes more sense. Quote Link to comment Share on other sites More sharing options...
s1yman Posted August 26, 2008 Author Share Posted August 26, 2008 *bump* Quote Link to comment Share on other sites More sharing options...
s1yman Posted September 9, 2008 Author Share Posted September 9, 2008 Anyone got any ideas? Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.