0

I currently have the following code on my website:

$(document).ready(function() {
$("#contact").on("click", function(e)
{ 
    e.preventDefault();
    $("#contactform").toggle('fast');
});
});

I would like to have an if(isset($_GET['email')); trigger this function as well, so have it open on page load if the $_GET variable is set.

I'm rather new with Jquery and not sure if this is possible, I also have another somewhat related question, I'm not sure if I should make a new question for this as I'm fairly new to stackoverflow as well, but here it is.

Say I have two of these:

$(document).ready(function() {
$("#contact").on("click", function(e)
{ 
    e.preventDefault();
    $("#contactform").toggle('fast');
});
});

$(document).ready(function() {
$("#archivestop").on("click", function(e)
{ 
    e.preventDefault();
    $("#archives").toggle('fast');
});
});

I want one to close if the other one is opened, how would I go about this?

Thanks!

1

3 Answers 3

2

Here's the Javascript-solution:

function getParam(key) {
        var paramsStr = window.location.search.substr(1, window.location.search.length),
            paramsArr = paramsStr.split("&"),
            items     = [];

        for (var i = 0; i < paramsArr.length; i++) {
            items[paramsArr[i].split("=")[0]] = paramsArr[i].split("=")[1];
        }

        if (key != "" && key != undefined) {
            // return single
            if (items[key] != undefined) {
                return items[key];
            } else {
                return null;
            }
        } else {
            // return all (array)
            return items;
        }
    };

if (getParam("email")) { 
    //    ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

Regarding your second question you can use the following to determine if an element is visible:

var bool = $('.foo').is(":visible");

So to hide an element if it is visible you would do something like this:

if ($('.foo').is(":visible")) {
    $('.foo').hide();
}

1 Comment

Thanks very much, this was very easy to understand.
0

I'm silly and have answered my first question. I still have yet to have my coffee.

The following works, just insert it into the div that is to be displayed:

<div id="contactform" style="<?php if(isset($_POST['email'])) echo "display:block;" ?>">
content
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.