0

After adding dynamically input element (with class "dateValidation") inside static div ( with class "workItems") I use this to provide onclick function:

$(".workItems").on("click", ".dateValidation", function()
{...});

and when I in function above run f.e

alert(this);

I get: [object HTMLInputElement] (I think it's good)

but, when I run:

alert(this.val());
alert(this.hasClass("dateValidation"));

Nothing happens. What is wrong with this code? How Can I get f.e value of this input element?

3 Answers 3

4

this is a DOM element, you need to convert it to jquery element before running jquery functions on it

alert($(this).val());
alert($(this).hasClass("dateValidation"));
Sign up to request clarification or add additional context in comments.

5 Comments

@nolbadi111 is your _checkingDate function expecting a DOM object or jquery object
$(".workItems").on("click", ".dateValidation", function() {_settingPlaceholderDate(this);} and function _settingPlaceholderDate(providedDate){ $(providedDate).attr("placeholder","dd-mm-rrrr");};
@nolbadi111 is this working fine? what is the question?
No, it doesn't work, but only on dynamically added input. When I use _settingPlaceholderDate(this) on static element everything is correct.
Didn't understand this. Can you add the actual problem you are facing in your question?
0

Use $(this).val() and $(this).hasClass("dateValidation"); instead of this.

Comments

0

this is the DOM object, whereas $(this) is the jQuery wrapper.

Using this, you can call DOM methods/attributes but not jQuery methods. And when using $(this), you can call jQuery methods not DOM methods.

In your case you're trying to access the jQuery method val() using DOM object this which is wrong. So you've to use jQuery methods using jQuery wrapper $(this).

Updated script would be like this.

alert($(this).val());
alert($(this).hasClass("dateValidation"));

Hope this will help to you find issue !!

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.