How to redirect to another page and specific div to click button in mvc

I have one page called Page1 that has a button.

<button onclick="redirecttodivofotherpage(); "></button>

Others Page2 have 3 Div

<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>

I want to redirect to div3 when clicking the Page1 button.
How to do this with a controller or jQuery.

+4
source share
4 answers

You can try something like this:

<button class="js-btn"></button>

$(function(){
    $(".js-btn").on("click",function(){
        window.location = "..../#div3";
    });
})

The string "..../#div3"represents the relative URL of your page and has at the end #div3. Thus, using window.location, you will be redirected to the desired page and with the help #div3to the desired section.

+4
source

cookie. cookie , , , , cookie . jquery-cookie.

. . "" .

**http://plnkr.co/edit/hBJj69nP6kvrEuoCVw3k?p=preview**
+4

try this working demo that will work

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
	<button class="click">Click Me</button>
	<div id="mydiv" style="border:2px solid black;width:800px;height:900px; background-color:orange; position:absolute;top:1000px;margin:20px;">
		hello anuradh
	</div>
</div>

<script type="text/javascript">
	$(document).ready(function(){
		$(".click").on('click',function(){
			window.location = "#mydiv";
		});
	});
</script>

</body>
</html>
Run codeHide result

otherwise you can scroll it just like that.

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
	<button class="click">Click Me</button>
	<div id="mydiv" style="border:2px solid black;width:800px;height:900px; background-color:orange; position:absolute;top:1000px;margin:20px;">
		hello anuradh
	</div>
</div>

<script type="text/javascript">
	$(document).ready(function(){
		$(".click").on('click',function(){
			//window.location = "#mydiv";

			 $('html, body').animate({
        		    scrollTop: $("#mydiv").offset().top
    				}, 2000);
			});
	});
</script>

</body>
</html>
Run codeHide result
+2
source

Use window.location.hash to scroll to an element with id

<button class="js-btn"></button>

$(function(){
    $(".js-btn").on("click",function(){
        window.location.hash = "#div3";
    });
});
+2
source

Source: https://habr.com/ru/post/1653969/


All Articles