Jump to content

Getting value from prompt


s1yman

Recommended Posts

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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);
    }

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.