Jump to content
Sign in to follow this  
w3schoon

JQuery inside JavaScript Prototype

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.

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...