0
if(getCookie("response") == null)
{
document.getElementById('hide').style.display = 'none';
alert("Yipee");
}

else
{

//on approve creating a new cookie
function a()
{
 var a = document.getElementById('approve');
 document.getElementById('hide').style.display = 'none';
 var expDate = new Date();
expDate.setDate(expDate.getDate() + 7);
document.cookie = 'response=approve;expires=' + expDate.toUTCString();
alert(document.cookie);
}

//on reject creating a new cookie

function r()
{
 var a = document.getElementById('reject');
 document.getElementById('hide').style.display = 'none';
 var expDate = new Date();
expDate.setDate(expDate.getDate() + 7);
document.cookie = 'response=reject;expires=' + expDate.toUTCString();
alert(document.cookie);
}

}

</script>

<body>
<div id="hide">
<form>
<p id="p">Heya!</p>

<input type="button" id='approve' value="approve" onclick="a()"/>
<input type="button" id='reject' value="reject" onclick="r()"/>
</form>

THis is my code...I want to hide an element if a cookie exists and if not, i want to go ahead and display the form. No matter what, the form is always displayed and the "yipee" alert box does not appear.

I check with Chrome and it has the cookie called response for localhost.

Thanks for all your help.

2
  • And where is your function getCookie? Commented Nov 14, 2010 at 12:32
  • And you are calling the code before the elements are rendered. View the JavaScript error console, I am sure you have tons of error messages. Commented Nov 14, 2010 at 12:35

2 Answers 2

2

maybe instead of getCookie use

c_start=document.cookie.indexOf("mycookie=");
if (c_start!=-1){
//cookie exists
}
Sign up to request clarification or add additional context in comments.

1 Comment

hey...it worked....thanks so much....but the element does not get hidden....only the alert box is displayed
0

If this is a code snippet from the page and it appears in that order, then this couldn't possible work. You test for the existence of the "response" cookie and then attempt to hide DOM elements before the DOM elements are there to hide in the first place.

Try moving the script block into the bottom of the body and see if that makes a difference. I think it probably will.

1 Comment

i want to hide the element "HIde" if a cookie is present.

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.