Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to clear the text box value

Status
Not open for further replies.

jambai

Programmer
Mar 21, 2007
58
US
I have two text boxes in a form.
The user can only enter text in one or the other, not both. If they enter data in one text box and there is data in the other, clear that other data.

Please let me know what event should I use.


Thank you
Jambai
 
A rough sketch. Up to you to change onblur to onkeypress.

Code:
<script>
function checkBox(inTextBox) {
    var boxId = inTextBox.id;

    if(boxId == "box1") {
        document.getElementById("box2").value = "";
    }else if(boxId == "box2"){
        document.getElementById("box1").value = "";
    }
}
</script>

<input type="text" id="box1" onblur="checkBox(this)"/>
<input type="text" id="box1" onblur="checkBox(this)" />
 
A little addition to Milkey's post, check to see if the user actually types something into the other text box. If they type something into BOX1 and then click on BOX2, only delete the contents of BOX1 if something is typed (that is assuming you use the onBlur)

Code:
<script>
function checkBox(inTextBox) {
    var boxId = inTextBox.id;
	[b]var boxValue = document.getElementById(boxId).value;[/b]

    if(boxId == "box1" [b]&& boxValue != ""[/b]) {
        document.getElementById("box2").value = "";
    }else if(boxId == "box2" [b]&& boxValue != ""[/b]){
        document.getElementById("box1").value = "";
    }
}
</script>

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top