Hello im sry if i have any of this code wrong, im not very good at Javascript.
so basically i have a ChgText() which works fine althou it replaces the whole field of my text area.
how would i go about changing this code so i could have it just insert without erasing the text field ?
Thank you

<script type="text/javascript" language="javascript"><!--


function ChgText()
{
var MyElement = document.getElementById("MyTextBox");
MyElement.value = ":-)"

return true;
}

//--></script>

and

 <img src=images/smilies/welcome.gif alt="Click Me!" onclick="ChgText()" />

Recommended Answers

All 9 Replies

Try

MyElement.value += ":-)";

yes!
Thank you so much Airshow
how did i miss that ...

Is there a way to send the value to a differant page, threw a popup with this same code ?

The simplest way to send value(s) to another page is to submit a form with the value(s) in <input> fields within the form.

As the value you wish to send is already in a text box (presumably an <input> field), then all youi have do is to to wrap the field in form tags (hard coded HTML on the page), and cause the form to be submitted. There's a couple of ways to do this.

Form with a Submit button:

<form action="myPage.php" method="get">
    <input id="MyTextBox" name="MyTextBox" value="initial text">
    <input type="submit" value="Go to myPage">
</form>

Form submission triggered from javascript:

<form action="myPage.php" method="get">
    <input id="MyTextBox" name="MyTextBox" value="initial text">
</form>

function ChgText() {
    var MyElement = document.getElementById("MyTextBox");
    MyElement.value = ":-)";
    MyElement.form.submit();
    return true;
}

Of course, you need to make provision for myPage.php (or myPage.html or whatever) to pick up the value of the parameter it has been sent. This will typically be a server-side responsibility but can be performed client-side if there's no appropriate server-side capability. But that's the subject of another question.

thank you so much Airshow !
how about from same page popup like this..

<!DOCTYPE html>
<html>
<head>
<script>
function openWin()
{
myWindow=window.open('','','width=200,height=100');
myWindow.document.write('<img src=images/smilies/welcome.gif alt="Click Me!" onclick="ChgText()" />');
myWindow.focus();

}
</script>
</head>
<body>

<input type="button" value="Open 'myWindow'" onclick="openWin()" />

</body>
</html>

althou it doesnt add text to the field when i click on the image.
thanks

True popup windows have become very unfashionable in recent years. They are awkward to work with as they contain a separate document and a separate javascript environment.

Instead, we now tend to use "modal dialogs", which are typically divs overlayed on the page in such a way that the page's normal content can't be interacted with while the dialog is displayed. To achieve this, you would use something like jQuery plus jQuery UI, which includes a "dialog" widget. (Other good javascript libraries also exist).

Modal dialogs make life much easier than true popups because they are implemented within the same DOM as the page's main content and share the same javascript environment. Thus, you can call your function from a button, or whatever, in the dialog in exactly the same way as you would from a button in the main content and you can hyperlink to a different page without needing to close a popup window explicitly.

Links :

Thank you

Thank you so much for your help Airshow
sry to keep adding to this thread but how can i add another value in this script

function ChgText()
{
var MyElement = document.getElementById("MyTextBox");
MyElement.value += ":-)"
MyElement.value += ":-0"
return true;
}

this adds both values.
Thank you

figured it out

<script type="text/javascript" language="javascript"><!--


function ChgText()
{
var MyElement = document.getElementById("MyTextBox");
MyElement.value += ":-)";

return true;
}
function ChgText2()
{
   document.getElementById('MyTextBox').value+=":-0"; 

    return true;
}

//--></script>

Thank you so much for your help Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.