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

How to use JQuery in Service Portal Widgets?

User477307
Tera Contributor

I am trying to create a simple responsive Nav Bar for Service Portal. I tried a Of-the-Shelf code available on the internet. pasted in my widget but the the JQuery part of it doesn't seem to be working. The entire functionality is broken and not working as expected.

 

I tried creating dependencies as well with JS include type URL and adding the CDN link in that field.

Can someone please answer this once for all I feel like lot of the FIRE-POWER for UI, which we find on the internet goes unutilized because of ServiceNow not able to work with external Libraries. 

 

Please let me know how solve this.

 

Below is the Code:

HTML

<nav>
  <div class="container">
    <ul>
      <li><a href="#">Home</a></li>
      <li> <a href="#">Categories </a>
        <ul>
          <li><a href="#">Category One</a></li>
          <li><a href="#">Category Two</a></li>
          <li><a href="#">Category Three</a></li>
        </ul>
      </li>
      <li><a href="#">Contact</a></li>
    </ul>
  </div>
</nav>

<script src="https://code.jquery.com/jquery-3.6.1.slim.js"
        integrity="sha256-tXm+sa1uzsbFnbXt8GJqsgi2Tw+m4BLGDof6eUPjbtk="
        crossorigin="anonymous"></script>
<script>
  $('nav li').hover(
    function() {
      $('ul', this).stop().slideDown(200);
    },
    function() {
      $('ul', this).stop().slideUp(200);
    }
  );
</script>

CSS

nav { background: #2ba0db; }

nav ul {
  font-size: 0;
  margin: 0;
  padding: 0;
}

nav ul li {
  display: inline-block;
  position: relative;
}

nav ul li a {
  color: #fff;
  display: block;
  font-size: 14px;
  padding: 15px 14px;
  transition: 0.3s linear;
}

nav ul li:hover { background: #126d9b; }

nav ul li ul {
  border-bottom: 5px solid #2ba0db;
  display: none;
  position: absolute;
  width: 250px;
}

nav ul li ul li {
  border-top: 1px solid #444;
  display: block;
}
nav ul li ul li:first-child { border-top: none; }
nav ul li ul li a {
  background: #373737;
  display: block;
  padding: 10px 14px;
}
nav ul li ul li a:hover { background: #126d9b; }

 

 

 

CLIENT SCRIPT

//Client Script
api.controller=function() {
	/* widget controller */
	var c = this;
	$(document).ready(function(){
		responsive_menu = $('.navbar_ul');
		$('#menu-acces').on('click', function(e) {
			e.preventDefault();
			responsive_menu.slideToggle();
		});
		$(window).resize(function(){
			var obtener_ancho = $(this).width(); 
			if(obtener_ancho > 480 && responsive_menu.is(':hidden')) {
				responsive_menu.removeAttr('style');
			}
		}); 
		$('nav li').on('click', function(e) {                
			var obtener_ancho = $(window).width();
			if(obtener_ancho < 480 ) {
				responsive_menu.slideToggle(); 
			}
		});
	});
};

Widget Dependencies.

User477307_0-1669705006992.png

 

RESULTS:

Reality

User477307_1-1669705179949.png

Expected:

User477307_2-1669705267070.png

Would be great if some one can help.

 

Thanks.

 

1 ACCEPTED SOLUTION

jaheerhattiwale
Mega Sage

@User477307 

100% working solution.

 

Replace HTML with following:

 

<nav>
<div class="container">
<ul>
<li><a href="#">Home</a></li>
<li> <a href="#">Categories </a>
<ul>
<li><a href="#">Category One</a></li>
<li><a href="#">Category Two</a></li>
<li><a href="#">Category Three</a></li>
</ul>
</li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</nav>

<script src="https://code.jquery.com/jquery-3.6.1.js"
integrity="sha256-tXm+sa1uzsbFnbXt8GJqsgi2Tw+m4BLGDof6eUPjbtk="
crossorigin="anonymous"></script>
<script>
$('nav li').hover(
function() {
$('ul', this).stop().slideDown(200);
},
function() {
$('ul', this).stop().slideUp(200);
}
);
</script>

 

 

Note:

I just changed "https://code.jquery.com/jquery-3.6.1.slim.js" to "https://code.jquery.com/jquery-3.6.1.js"

 

Result:

jaheerhattiwale_1-1669875002906.png

 

 

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

17 REPLIES 17

User477307_0-1669716740575.png

see..

 

Yes, that is expected.
So you can replace $ with jQuery and check whether it works

 

okk..

https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_dropdown_navbar now I'm just trying to implement this simple code which does not even have jQuery but the dropdown thing doesn't work.. not sure why..!

@User477307 So your problem is not related to jQuery. jQuery is available to use in the service portal. Please like my answer if helpful.

Well the original question is related to jquery.. was just trying different stuff I've given you the code, can you please try in your PDI?