0

wondering if this is possible, currently using this to click a 'clear' button, then it clears UPIN;

Protected Sub clear(ByVal Sender As Object, ByVal e As System.EventArgs)
UPIN.Text=String.Empty
End Sub

But I want it to clear whichever textbox is in focus! for example;

focusedstring.Text=String.Empty

Again is it possible without masses of JS or anything?

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
Kieronboz
  • 85
  • 3
  • 14
  • You can find the answer here! http://stackoverflow.com/questions/4135818/how-to-clear-a-textbox-using-javascript This shouldn't be too hard to adapt to your needs :) And I clicked delete instead of Edit a second ago :( – BobbyDazzler Nov 18 '13 at 13:18

2 Answers2

3

You should do it on client side (edit: as suggested by MightyLampshade you can find on of 1000 examples here), you do not need a round-trip to server for that. If you have, let's say, a clear button:

$("#clear").click(function() {
    $(".can-be-cleared").val("");
});

Please note that this will clear all elements with class can-be-cleared (I assume you may do not want to clear each input but a specific set, if it's not the case just replace it with input[type=text]).when you click an element with id clear.

If each "clear" button has to be tied with a single specific text box then you have to repeat them (because when you click on the button textbox won't be focused any more). As alternative you may remember last focused text box. Let's see both:

<input type="text" id="textbox1"/>
<button class="clear-button" data-textbox="textbox1">clear</button>

JavaScript for this is:

$(".clear-button").click(function() {
    $("#"+$(this).data("textbox")).val("");
});

Simpler alternative (I prefer this if you do not have any other special requirement) may be to keep track of last focused textbox:

var lastFocused = undefined;

$("input[type=text]").focus(function () {
    lastFocused = $(this);
});

$("#clear-field").click(function () {
    if (lastFocused !== undefined) {
        lastFocused.val("");
    }
});

Of course do not forget to match $("#clear-field") with right ID you used for your clear button, in this case I assume:

<button id="clear-field">Clear</button>

Anyway if you really need some server side processing (for any other reason), TextBox that generated event is in sender parameter:

Dim textbox As TextBox = DirectCast(sender, TextBox)
textbox.Text = String.Empty
Community
  • 1
  • 1
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
  • Hi, there are 3 fields, A B and C, all edited in turn, I want to clear whichever is in focus so they dont need to do A and B again if they are on C. Currently trying, does this line seem correct; $(input[type=text]).val(""); Thanks – Kieronboz Nov 18 '13 at 13:28
  • @Kieronboz yes, if you have just three textboxes you can omit class in query so it'll be (don't forget quotes): `$("input[type=text]").val("");` (and don't forget to change `#clear` to the right ID of your clear button). What I don't understand is: **do you want to clear them with a clear button or when they receive focus**? – Adriano Repetti Nov 18 '13 at 13:31
  • Hi again, I want them to clear if they want too due to a mistake, so they are inputting data, they get it wrong, click clear, it deletes the characters in the field they are typing in. Rather than just using UPIN.Text=String.Empty. 3 times for ABC – Kieronboz Nov 18 '13 at 13:35
  • @Kieronboz ok, got it. There are (at least!) two options for that, I updated answers with some code examples. – Adriano Repetti Nov 18 '13 at 13:53
-1

can you try this:

 $('input[type=text]').focus(function() {
      $(this).val('');
      });

Its simple with Jquery..no need to write masses of lines of code.

Updated:

If you do want to Clear on button click:

  $("#Clear").click(function(){
    $('input[type=text]').focus(function() {
      $(this).val('');
      });    
});
Sai Avinash
  • 4,683
  • 17
  • 58
  • 96