Introduction
Hi all. Today I came across a situation of disabling and enabling a href tag programmatically. The property enable and disable is applicable for the input elements. So what must we do?
Using the code
Let us say I have an a tag as follows:
- <a href="#" id="btnCreate"></a>
Now what we will do is just add an attribute and set the value as follows.
- if (data.length > 1000)
- {
- $('#btnCreateWidget').attr('disabled', true);
- }
Here (data.length > 1000 ) is my condition. You can provide your own condition there. Now what do we need to think? What if the condition fails? We then must remove the attribute, right?
- else
- {
- $('#btnCreateWidget').removeAttr('disabled');
- }
Now the remaining part is handling this in the click event. Are you ready?
Please see the following code.
- $("#btnCreate").click(function (e)
- {
- if ($(this).attr('disabled') != undefined)
- {
- return ($(this).attr('disabled')) ? false : true;
- }
- else
- {
- //else condition codes here
- }
- }
What we are doing in the preceding code is we are checking whether the attribute we added is undefined or not first. Then if the attribute value is disabled, we are returning false. In the else part we can write the necessary code.
Conclusion
I hope you liked this article. Please provide your valuable suggestions.
Kindest Regards,
Sibeesh venu
www.sibeeshpassion.com
Gowtham RajamanickamTOP 100Posted Apr 25, 2015, 2:08 AM EDT
good
Gowtham RajamanickamTOP 100Posted Apr 25, 2015, 2:08 AM EDT
good
Sibeesh VenuTOP 100Posted Apr 13, 2015, 10:35 PM EDT
Santhakumar Munuswamy I am glad you liked it.
Sibeesh VenuTOP 100Posted Apr 13, 2015, 10:35 PM EDT
Thank you Santhakumar Munuswamy
Santhakumar MunuswamyPosted Apr 13, 2015, 10:39 AM EDT
it is simple and easily understand
Santhakumar MunuswamyPosted Apr 13, 2015, 10:39 AM EDT
Thanks for sharing.