Navin
Navin

Reputation: 193

jQuery Attribute Equals selector is not working

jQuery Attribute Equals Selector is not working. Please, have a look into it. Thanks in advance. :)

<!DOCTYPE html>
<html>
	<head>
	<title>My Page</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css" />
	<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
	<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>
	<script type="text/javascript" >
		
	$( document ).ready(function() {
    	alert($( "input[myattr='navo']" ).value);
		$( "input[myattr='navo']" ).value="Simon Commission";
		alert($( "input[myattr='navo']" ).value);
	});
   </script>
</head>
<body>
	<input type="text" myattr='navo' value="Morley Minto Reform" />
</body>
</html>

Upvotes: 0

Views: 495

Answers (3)

Arun P Johny
Arun P Johny

Reputation: 388326

$( "input[myattr='navo']" ) returns a jQuery object, not a dom element so it doesn't have a property called value. You need to use the various methods provided by jQuery on the jQuery object.

In this case you can use the .val() method to get/set the value of an input element

$(document).ready(function() {
  alert($("input[myattr='navo']").val());
  $("input[myattr='navo']").val("Simon Commission");
  alert($("input[myattr='navo']").val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" myattr='navo' value="Morley Minto Reform" />

Upvotes: 2

Trevor
Trevor

Reputation: 13457

The .value property doesn't exist on a jQuery object. If you want to access the value you have to call the .val() function; if you want to change the value, you have to pass the new value into the .val("some value") function.

<!DOCTYPE html>
<html>
	<head>
	<title>My Page</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css" />
	<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
	<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>
	<script type="text/javascript" >
		
	$( document ).ready(function() {
    	alert($( "input[myattr='navo']" ).val());
		$( "input[myattr='navo']" ).val("Simon Commission");
		alert($( "input[myattr='navo']" ).val());
	});
   </script>
</head>
<body>
	<input type="text" myattr='navo' value="Morley Minto Reform" />
</body>
</html>

Upvotes: 2

VPK
VPK

Reputation: 3090

Try this,

alert($( "input[myattr='navo']" ).val());

in jQuery value can be get using the method val(), it doesn't have any property like value.

Upvotes: 5

Related Questions