Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery 3.4.1 Cannot read property 'value' of undefined

After upgrade jQuery 3.4.0 to 3.4.1, on blur() or focus() events on input I'm getting Cannot read property 'value' of undefined error.

that.container.on('click', '.binary_field', function(){
    $("input:focus").blur();
}

I think that may be cause of this change in jquery source code: https://github.com/jquery/jquery/commit/24d71ac70406f522fc1b09bf7c4025251ec3aee6?diff=unified#diff-031bb62d959e7e4949d1847c82507f33L579-R583

like image 939
Unknown123 Avatar asked Feb 12 '20 13:02

Unknown123


People also ask

How do you fix undefined properties Cannot be read?

The “cannot read property of undefined” error occurs when you attempt to access a property or method of a variable that is undefined . You can fix it by adding an undefined check on the variable before accessing it.

What is TypeError Cannot read properties of undefined?

Causes for TypeError: Cannot Read Property of Undefined In short, the value is not assigned. In JavaScript, properties or functions are Objects, but undefined is not an object type. If you call the function or property on such variable, you get the error in console TypeError: Cannot read undefined properties.

Can not read the property of null?

The "Cannot read property 'click' of null" error occurs for 2 reasons: Calling the click() method on a null value, e.g. a DOM element that doesn't exist. Placing the JS script tag above the HTML where the DOM elements are declared.


2 Answers

This is a known issue in jQuery 3.4.1:

https://github.com/jquery/jquery/issues/4417

Use the JavaScript .blur() instead of the jQuery method.

like image 54
Ezra Avatar answered Oct 20 '22 08:10

Ezra


Your code seems good, there is a workaround in your case, remove jquery for this line.

You can replace

$("input:focus").blur()

by

document.activeElement && document.activeElement.blur() 
// Test if there is a focused element, if yes remove the focus

or (with jQuery and VanillaJs)

$(document.activeElement)[0].blur()
// or
$("input:focus")[0].blur()

It's the same behavior

like image 5
R3tep Avatar answered Oct 20 '22 08:10

R3tep