Jump to content

JQuery inside JavaScript Prototype


w3schoon

Recommended Posts

Nice be with you everyone! My problem is: How to put JQuery inside JavaScript Protoype? Here's my code:

	    <script type="text/javascript">	   		    $(document).ready(function(){			   			    String.prototype.maxlength=function(a){				    return this.attr("maxlength", a);			    }			   			    $("#name").maxlength(32);			   		    });	    </script>

And this...

	   <body>		    <p>Name: <input id="name" type="text" name="name" value=""></p>		    <input type="submit" value="submit">	   </body>

My reason of using this prototype is to simplify my codes. Thank you.

Link to comment
Share on other sites

it would be helpful if you could explain what you are expecting to happen, vs what is happening. I'm pretty confused as to what you're trying to do? Are you trying to make it so that when a user inputs data, you stop letting the user input data after a predetermined length? A couple of issues I see regardless1) In order to get the value of an input element in jquery, you need to call .val() on it. 2) With that in mind, right now you are just trying to call maxlength on a jquery object, not a string object. 3) Where is the attribute max length coming from? I'm not sure you know what this means in Javacript. In this context this refers to the string object itself. When used on a actual string, it would be the value of the actual string itself. Is this something like what you were looking for?http://jsbin.com/uravop/1/edit

Edited by thescientist
Link to comment
Share on other sites

Thank you for the reply! Finally by experimentation I solved the problem on how to put JQuery inside JavaScript Protoype What am I trying to do is to make a Javasscript prototype that maximums number of characters allowed in the <input> element using maxlength. To simplify my code I decided to use attr() method, unfortunately It didn't work, but blessedly by trial and error I found out that my problem is simple. Use "String.prototype" if your constructor is for String e.g. length(), replace(), etc. Use "Object.prototype" if your constructor is for Object e.g. attr() etc. Here's my code and it's working! Try it yourself >>

<!DOCTYPE html> <html>  	<head>		<title>Applicant Tracking Database System</title>		<script type="text/javascript" src="javascripts/jquery-1.9.1.min.js"></script>		<script type="text/javascript">					$(document).ready(function(){								Object.prototype.maxlength=function(a){					this.attr("maxlength",a);				};								$("#name").maxlength(10);			});		</script>	</head>	<body>	   <p>Name: <input id="name" type="text" name="name"></p>	</body></html>

Thank you!

Edited by w3schoon
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...