Konstantin
Konstantin

Reputation: 15

How to change css of the element from another page using jquery?

Please, help me! I have two html pages. There is a button on the first page and a div on the second page. So, when I click the button, I want it to open the second page in the same window and change the background of the div.

Here is my code :

$(".button").on("click",function(){

    window.location.href="secondPage.html";
    $("div").css({background: "red"})

});

Any suggestions?

Upvotes: 0

Views: 2325

Answers (3)

empiric
empiric

Reputation: 7878

You can do something like:

First page

$(".button").on("click",function(){

    localStorage.setItem('button', 'clicked');
    window.location.href = "secondPage.html";

});

Second page

if(localStorage.getItem('button') === 'clicked'){
    $("div").css({backgroundColor: "red"});
}

To remove the Item in the localStorage-Object (if the background should not be changed conditionally) use:

localStorage.removeItem('button');

Small Demo

Reference

Note: the localStorage-Object is only available in IE Version > 7. The other major-browser will work.

Upvotes: 1

Eduardo Vélez
Eduardo Vélez

Reputation: 193

The code should be placed on second page, and from that call the object "Parent" to ask whether it is null or not, in this case, if it is null, you can validate using the HREF property of the PARENT object to see if it is correct page and then change the color of the DIV.

Code in second HTML like to:

$(document).ready(function() {
if (windows.document.parent != null)
{
$("div").css({background: "red"});
}
});

Upvotes: 0

Ashwath
Ashwath

Reputation: 407

Use query string... When you click the button along with URL pass a query string window.location.href="secondPage.htm?src=btnClickl";

And in second page.HTML look for SRC query string. If pressent , change the div color...

Good luck...

Upvotes: 0

Related Questions