0

I need a smart solution for setting textareas attributes. I do have a lot of textareas at the page. Some are disabled and some are not. It depend on user logged in and selections made. All of the textareas are 'required' by custom class 'class="reqformtextbox validate[required]"',

What I want is to scroll/find textboxes and set its class to class="reqformtextbox", effectively removing 'required' attribute for the textboxs which are currently disabled by 'disabled="disabled"' attribute.

2 Answers2

1

Assuming enabeditable is an attribute with boolean values (correct me if I'm wrong), the jQuery you want is:

$('textarea[enableditable=true]').attr('required', true);

$('textarea[enableditable=true]') will select all elements with tag textarea and attribute enableditable=true

attr('required', true) will add or replace attribute required=true.

Javascript doesn't have a one-line selector for html tag + attribute, but you can make a function that queries all elements by getElementsByTagName(), iterates them and returns a list with all elements having `getAttribute('enableditable') = true.

Alexandru Severin
  • 6,021
  • 11
  • 48
  • 71
  • I can't select all disabled textareas with getElementsByTagName(). I can olny select all textareas. regardless of it's attribute – Oleg Agadjanyan Sep 11 '15 at 10:10
  • @OlegAgadjanyan I wrote you the `getAllElementsWithAttribute` function – Alexandru Severin Sep 11 '15 at 10:13
  • Sorry for the mess but since my first post my task slightly changed and i have edited the question. now the requirement is to apply different class to disabled elements. The idea is the same though. removing 'required' attribute. – Oleg Agadjanyan Sep 11 '15 at 13:13
  • @ Alexandru....I m getting 'TypeError: all[i].getAttribute is not a function' error using the function – Oleg Agadjanyan Sep 12 '15 at 13:27
  • @OlegAgadjanyan just use the jquery solution, much easier to implement and understand. – Alexandru Severin Sep 13 '15 at 07:59
  • Gents, I have solved the problem with Jquery. Thank you all for pointing me into the right direction. `jQuery(document).ready(function(){ jQuery('textarea[disabled="disabled"]').removeClass( "reqformtextbox validate[required]" ).addClass( "reqformtextbox" ); }); ` – Oleg Agadjanyan Sep 13 '15 at 15:52
1

Yes, you can use jQuery to solve this.

Here is the example

$(function(){
   $("input[type=text]").attr("required","required");
});

Replace selector if you have any other selector. This will apply for all textbox in the page.

Sushmit Patil
  • 1,335
  • 1
  • 14
  • 26