简体   繁体   中英

hide a div using javascript using if statement

I want to hide a div with id apDiv1 using javascript when the values are not equal, I am using the following code.But it is not working.

<script>
/* <![CDATA[ */
if (#{sessionScope['userdet']['email']}!=#{sessionScope['frienddet']['email']}){
$('#apDiv1').hide();
}
/* ]]> */
</script>

The rendered code is as follows:

<script>
    /* <![CDATA[ */
    if (amlan@ymail.com != atinr4@gmail.com){
     $('#apDiv1').hide();
    }
    /* ]]> */
</script>

The rendered code is not valid JS - you've got a syntax error on the if condition because you need to quote the strings that contain the email addresses. That is, your rendered code should look like this:

if ("amlan@ymail.com" != "atinr4@gmail.com"){

Or you can use single quotes, JS allows either (as long as they match for any particular string literal).

I don't know what your source language is, but I assume you could get the rendered result that you need with something like this:

if ("#{sessionScope['userdet']['email']}"!="#{sessionScope['frienddet']['email']}"){

EDIT: Have you included the jquery.js file on your page? You didn't use the jquery tag on your question, but the $("#apDiv1").hide() part uses jQuery functions $() and .hide() . If you don't have jQuery try something like this:

document.getElementById("apDiv1").style.display = "none";

Also, the code you've shown will only be able to find the div if the script block appears after it, or if you wrap your code in a document ready handler as follows (though this also assumes jQuery is available):

$(document).ready(function() {

    // your other code here    
});

Non-jQuery version using an onload handler:

window.onload = function() {

    // your other code here
};

您需要将2(电子邮件)值括在引号中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM