3

I have 2 textBox and 1 button! I want to insert text to one of these textboxs. When I click to textbox_1 and click button, mytext will appear at textbox_1. When I click to textbox_2 and click button, mytext will appear at textbox_2. How can I do this by using JavaScript? Please help me! I'm new on JavaScript!

4 Answers 4

6

put id's of the two textboxes as textbox_1 and textbox_2 and put onclick='onCLickButton();' on the <button> tag and write the following code in the script

var text_to_be_inserted = "sample";
function onCLickButton(){
    document.getElementById("textbox_1").value='';
    document.getElementById("textbox_2").value='';
    if(document.getElementById("textbox_1").focused){
        document.getElementById("textbox_1").value=text_to_be_inserted;
    }
    else if(document.getElementById("textbox_2").focused){
        document.getElementById("textbox_2").value=text_to_be_inserted;
    }
    else{
        // do nothing
    }
}

Edited

Please accept my apologies actually I am used to use these functions as I have my own js file having these functions.

please add onfocus='onFocusInput(this);' in the <input> tags and add the following code in the script

function onFocusInput(object){
    document.getElementById("textbox_1").focused=false;
    document.getElementById("textbox_2").focused=false;
    object.focused = true;
}
Sign up to request clarification or add additional context in comments.

2 Comments

It not work, I focus to textbox_2 but when i click button, text appear at textbox_1! Help me!
When you click the button, focus changes to the button - so neither text box has the focus any more. That's why it doesn't work.
2
<html>
<head>
    <script type="text/javascript">
        var index = false;
        var text = "This text shifts to text box when clicked the button";
        
        function DisplayText(){
            if(!index){
                document.getElementById("txt1").value = text;
                document.getElementById("txt2").value = "";
            }
            else{
                document.getElementById("txt2").value = text;
                document.getElementById("txt1").value = "";
            }
        
            index = index ? false : true;
        }
   </script>
</head>
<body>
    <input type="text" id="txt1"/>
    <input type="text" id="txt2"/>
    <input type="button" value="Change Text" onclick="DisplayText()"/>
</body>
</html>

Comments

1

Take a look at the onFocus() attribute for the INPUT tag - and think about keeping track of what was last given the focus. I'm being a little vague as this sounds a lot like homework.

2 Comments

thank you! But can you explain more! I have a html form with multiple text field inputs. When I click button, it will insert text to textBox that I focused before, can I use onBlur event for my textBox?
Clicking the button is the trigger that fills the text box, so no need for onBlur(). If you remember which text box last got the focus - by writing an onFocus() handler for each box and using a global variable - then when you click the button and handle its onClick(), you'll know which box to write the text in.
1

It isn't the prettiest / most delicate solution, but it works and you can build off it to fulfill your needs.

<script>
var field = 0;
  function addText(txt){
     if(field === 0) return false;
     field.value = txt;
  }
</script>

For a form such as

<form>
<input type="text" name="box1" id="box1" onfocus="field=this;" />
<input type="text" name="box2" id="box2" onfocus="field=this;" />
<input type="button" onclick="addText('Hello Thar!');" />
</form>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.