The CreatorCon Call for Content is officially open! Get started here.

Get Element by Class Name not working

Daryll Conway
Giga Guru

I'm trying to grab an element by it's class name in a client script (Dublin) with

var mand = document.getElementByClassName('mandatory');

How ever this falls over with the error...

"TypeError: document.getElementByClassName is not a function"

Any thoughts why this is happening?

1 ACCEPTED SOLUTION

Mark Stanger
Giga Sage

How about 'getElementsByClassName'?   I think it needs to be plural.


View solution in original post

4 REPLIES 4

Daryll Conway
Giga Guru

This is the whole script



function onLoad() {


      var closeCode = String(g_form.getValue('u_closure_code'));


      if(g_form.getValue('state') != 1){


              g_tabs2Sections.setActive(1);


              g_tabs2List.setActive(1);


      }


      alert('pre mand');


      var mand = document.getElementByClassName('mandatory');


      alert(mand);


      if(mand.style.indexOf('visible') != -1)


              {


              if(closeCode != '' && closeCode != 'perfect_change'){


                      g_tabs2Sections.setActive(2);


                      g_tabs2List.setActive(1);


              }


      }


}


Mark Stanger
Giga Sage

How about 'getElementsByClassName'?   I think it needs to be plural.


That's the one, cheers Mark I'd completely overlooked that


Pastupe
Mega Guru

Hello,


I had simmiliar requirement.


Goal was to not display ticket number in the form header until ticket wasnt created -


Target was to hide ticket number in the form header, img below



First I viewed source code of HTML page and found div ID and all other relevant div class names that were under main DIV. When I had that information, I was able to hide different elements under the div, script is below



It was OK for me to put script in to UI policy and run script for new records only - this was requirement


Thru UI policy, number field was also hidden by common UI policy approach



Thru this approach (script below) you can easily have under control full screen. What you need to know is just div id and relevant class name , then you can do styling as needed.


hope this helps



/Petr



// Get the element with id="incident.form_header" (a div), then get all elements inside div with class="navbar-header"


var x = document.getElementById("incident.form_header").querySelectorAll(".navbar-header");  



// Set the visibility of the first element with class="navbar-header"


x[0].style.visibility = "hidden";



find_real_file.png