jquery function val is not equivalent to this value

0 votes

When I try to set a text input to blank (when clicked) using $(this).value="", this does not work. I have to use $(this).val('').

Why? What is the difference? What is the mechanism behind the val function in jQuery?

$(document).ready(function() {
    $('#user_name').focus(function(){
        $(this).val('');
    });
});
// error code: not working...
$(document).ready(function() {
    $('#user_name').focus(function(){
        $(this).value='';
    });
});

Jun 13, 2022 in JQuery by gaurav
• 23,260 points
2,936 views

1 answer to this question.

0 votes

val() method is primarily used to get the values of form elements such as input , select and textarea . When called on an empty collection, it returns undefined . When the first element in the collection is a select-multiple (i.e., a select element with the multiple attribute set)

answered Jun 14, 2022 by rajatha
• 7,680 points

Related Questions In JQuery

0 votes
1 answer
0 votes
1 answer

jQuery fix for "Uncaught TypeError: $ is not a function" error

We can fix this error by using jQuery() . ...READ MORE

answered Jun 1, 2022 in JQuery by Edureka
• 13,690 points
5,911 views
0 votes
1 answer

jQuery UI " $("#datepicker").datepicker is not a function"

The "$(...).datepicker is not a function" jQuery ...READ MORE

answered Jun 6, 2022 in JQuery by Edureka
• 13,690 points
15,515 views
0 votes
1 answer

$ is not a function - jQuery error

The typeerror: $ is not a function ...READ MORE

answered Jun 6, 2022 in JQuery by Edureka
• 13,690 points
1,331 views
0 votes
1 answer

How to use jquery with asp.net ajax?

If you weren't aware, Microsoft is planning ...READ MORE

answered Oct 15, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
764 views
0 votes
1 answer

Is 'sparkline' a method?

I suggest you to check 2 things That jquery.sparkline.js is actually ...READ MORE

answered Nov 9, 2018 in Apache Spark by Frankie
• 9,830 points
1,301 views
0 votes
1 answer

What is jQuery?

Hey, jQuery is a fast and concise JavaScript ...READ MORE

answered Feb 14, 2020 in JQuery by kartik
• 37,520 points
1,348 views
0 votes
1 answer

Error: Global Variable is not accessable to local function

Hey kartik, A variable declared outside a function has a ...READ MORE

answered Feb 19, 2020 in PHP by Niroj
• 82,840 points
1,106 views
0 votes
1 answer

Jquery validation plugin - TypeError: $(...).validate is not a function

The "$(...). validate is not a function" ...READ MORE

answered Jun 28, 2022 in JQuery by rajatha
• 7,680 points
16,868 views
0 votes
1 answer

Is this the Correct Way to Access Array Elements in jQuery?

Array elements are accessed by using an integer ...READ MORE

answered Jun 13, 2022 in JQuery by rajatha
• 7,680 points
1,413 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP