Too many scripts for a single action

Hello everyone and thank you for your precious advice. I dare to submit a request to you. On the attached project, there is a menu, each element of which contains a “target” script, a link to a specific location on the page. Is it possible to design a single script to manage all the targets and avoid the large number of individual scripts?
Be forgiving for my rough English.
Thank youatelierdu84.hype.zip (1.6 MB)

The trick could be, to give your buttons a class name. You can identify your target on base of this class name:

	var myButton = element.classList[1]; // identify target by className
	
	  	var target = $('#target'+myButton); // compose your target
  	$('html,body').animate({
      scrollTop: target.offset().top  
      // this value sets the speed (1000 is 1 second)  
    }, 1000);

This way, you have to use just only one single function.

Hope, that helps

atelierdu84_v1.hype.zip (2.2 MB)

2 Likes

Splendide !! Mille mercis !
Splendid !! Many thanks !

1 Like

On top of @ktewes good suggestion of using a single class as an ident,
You can also use the additional (data) attributes for each button.
This then should allow you to always target the button even if at a later date you need to add other classnames, which would possibly mean you need to adjust the code to the correct index number for the class name.

code :
var myButton = element.dataset.buttonid

Button attrib:

1 Like

Woo! I just bought the book “javascript for dummies” and I note with enthusiasm the way that I still have to go to be autonomous. Thank you both for these beneficial lessons.

2 Likes

Well, I‘m faaar away from being autonomous, but I learned a lot, lot here in the forum. Just read the posts carefully, analyze the examples - there are so many really reeally great JavaScript people on the forum…

1 Like

This forum is indeed a magnificent playground and learning ground. Thanks again.
Kenavo !

1 Like