Monday , February 3 2025

Find anchor in URL and scroll to its id on this page using JS

Here is a case: if a link contains a word, then direct the user to some anchor on the page. I needed this for ChronoForms in Joomla – when filling out the form, the page was updated, and it was necessary to return the user to filling out the form or viewing the status of sending the letter.

Here is the solution:

<script type="text/javascript">
jQuery(document).ready(function ($) {
	$(document).ready(function () {
    	if(window.location.href.indexOf("chronoform") > -1) {
      	 	const el = document.getElementById('chronoform_request');
			el.scrollIntoView();
    	}
	});
});
</script>

where chronoform is a piece of the link that was added after filling out the form, and chronoform_request is the ID of the form that needs to be scrolled to.

This one:

jQuery(document).ready(function ($) {
***
});

is needed to avoid jQuery errors $ is not a function

About iryna

I'm Iryna, a web developer from Ukraine with a decade of experience solving complex technical challenges in the world of freelance. Throughout my career, I've worked on everything from troubleshooting server-side issues and optimizing website performance to enhancing user interfaces. On this blog, I share detailed solutions to the technical problems I’ve encountered and methods that have worked best for me. In addition to my technical expertise, I’m also passionate about digital drawing. I hope the tutorials and insights I provide here will help both fellow developers and creatives alike in their own projects.

Check Also

How to get a link in an email where the form is filled out via Chronoforms

This task is very simple, for normal CMS. For Joomla, I publish the solution in …

Leave a Reply

Your email address will not be published. Required fields are marked *