1

In order to empty the contents of a table cell, I issue the following command:

$('td').empty()

but to empty the contents of an input field, I say:

$('input').val() -- Edit: .val('')

Q: Doesn't this go against the idea of object oriented programming? Wouldn't you have the same method for each which means "Whatever you need to do to empty yourself, you now have my permission to do so"?

Like, if you have a cat.speak and a dog.speak and the cat meows and the dog barks.

7 Answers 7

7

1: jQuery is NOT OOP and never claimed to be
2: $('input').val() just returns the value of the input, it does not empty it out
3: $('td').empty() removes html content. It is not made to "empty" out any input element, to empty an input you would do: $('input').val('')

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

Comments

2

Keep in mind that these operations are doing two very different things.

  • empty is removing HTML content from the target element(s).

  • val is setting the value of a form element, at least when it is used with an argument.

So I think it is fine that two differently-named functions are specified.

For what it's worth, jQuery is written in a prototypical/functional style, not OO, but that does not change your original question.

2 Comments

can you justify functional style? The heart of jQuery is $.fn which is all about prototypical inheritance
@JustinEithier "jQuery allows you to write functional code" and "the jQuery code is functional" are disjoint
2

Your confusing removing HTML content and setting a value to null.

you never remove text content from an input. The browser removes it for you due to it being data bound.

$("input").val("") does not empty the input.

The fact that .val("") removes text content from an input is a mere side effect that the text in an input field is data bound to the value property of the <input>.

Your generalizing too much.

Comments

1

Strictly speaking, they do entirely separate things.

.empty() removes all descendents of the specified node.

.val() in fact doesn't empty the contents of an input field, it returns them.

Comments

1

your empty and val are two different things .empty removes dom elements from the selector .val() gets the value of the input selector

Comments

1

I don't believe it's going against OOP since empty() exists to clear all DOM children from the given parent. While val() exists for settings values on controls that display text in some way, like buttons, textboxes etc.

So, td can have many children and empty() makes all sense when clearing it. While a button only contais a text value, wich makes sense to modify that val().

I think that here the OOP is ok.

Comments

1

Well, when you do empty you are cleaning everything in between a <td> and a </td>.

When you do .val('') you are setting the value attribute of the input field to empty.

<input type="text" value="hi"/>

Two diferent things.

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.