1

let's say I have this set of HTML-markup and CSS

#CSS

.inputhelp_text { background: #000; color: #fff; }
.nodisplay { display: none; }

<input class="inputhelp" id="firstname" /><span class="inputhelp_text nodisplay" id="help_firstname">Write in your firstname</span>

<input class="inputhelp" id="lastname" /><span class="inputhelp_text nodisplay" id="help_lastname">Write in your lastname</span>

Using jQuery, I need to bind a function to all input fields (I guess using jQuery's EACH function) so that when I click the input field, it should switch the class of each span to only "inputhelp_text". I've gotten this to work in two separate functions for each field, but since I have alot of fields, I know there's a better way to solve it.

Any suggestions?

3
  • rtfm: api.jquery.com/input-selector Commented Mar 23, 2010 at 8:52
  • I'm not used to jQuery, so even with a manual things start out slow. "Thank" you. Commented Mar 23, 2010 at 9:35
  • that why reading the documentation might be useful. Commented Mar 23, 2010 at 10:09

3 Answers 3

2

You want to bind handlers to the blur() and focus() events:

$(".inputhelp").focus(function() {
  $("span.inputhelp_text:visible").addClass("nodisplay");
  $(this).next("span.inputhelp_text").removeClass("nodisplay");
}).blur(function() {
  $("span.inputhelp_text").addClass("nodisplay");
});

Normally I'd recommend using jQuery effects like hide() and show() but these only really work with block level elements. <span> are inline so this won't work.

Sign up to request clarification or add additional context in comments.

1 Comment

@bakkelun details would undoubtedly be useful
1

You can use the each function:

$("input").each(function() {
  // this points to the input DOM element
  // $(this) is the jQuery wrapped element
});

For example you could have:

$(this).removeClass("inputhelp");
$(this).addClass("inputhelp_text");

inside the callback.

3 Comments

Could this work as well?? To select all inputs with class "inputhelp" ? $('input[class=inputhelp]').bind('focus',function() { // do something }
Yes, but $("input.inputhelp") or $(".inputhelp") are more simple selectors that do the same thing.
Please remember that shorter (more non-spesific) triggers makes the search for it inside the DOM slower. Much like xpath queries; specifying a longer more spesific path speeds up the query.
0

What I do in this situation is the following. Let us say that you have a set of $("input")'s that you want to bind to in some way, for example you want to add a class of .invalid when they are empty:

$("input").each(function () {
      var $this = $(this);
      $this.focus(function (e) {
        $this.removeClass("invalid");
      });

      $this.blur(function (e) {
        if ($this.val() == "") {
          $this.addClass("invalid");
        }
      });
    });

By storing a reference to $(this) returned from $.fn.each, you can now bind to events on each of the elements individually.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.