0

How i can set the innerhtml of a div,which represents a snackbar, as a parameter in javascript function from code behind? Problem: Problem is i can use the snackbar but i have to set its text manually like "the operation is successful". It works. But i want to make it versatile so the innerhtml is a variable rather than a static text set each time. Thanks

<style>   
  #snackbar {
    visibility: hidden;
    min-width: 250px;
    margin-left: -125px;
    background-color: #333;
    color: #fff;
    text-align: center;
    border-radius: 2px;
    padding: 16px;
    position: fixed;
    z-index: 1;
    left: 50%;
    bottom: 30px;
    font-size: 17px;
}

#snackbar.show {
    visibility: visible;
    -webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s;
    animation: fadein 0.5s, fadeout 0.5s 2.5s;
}

@-webkit-keyframes fadein {
    from {bottom: 0; opacity: 0;} 
    to {bottom: 30px; opacity: 1;}
}

@keyframes fadein {
    from {bottom: 0; opacity: 0;}
    to {bottom: 30px; opacity: 1;}
}

@-webkit-keyframes fadeout {
    from {bottom: 30px; opacity: 1;} 
    to {bottom: 0; opacity: 0;}
}

@keyframes fadeout {
    from {bottom: 30px; opacity: 1;}
    to {bottom: 0; opacity: 0;}
}
        .auto-style3 {
            margin-left: 56px;
        }
    </style>

<div><div id="snackbar"></div></div>

<script>
   var showSnack= function (txt,x) {
         x = document.getElementById("snackbar")
         x.innerHTML = txt;
        x.className = "show";
        setTimeout(function () { x.className = 
x.className.replace("show","text"); }, 4000);
    }
</script>

codebehind

ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text",
"showSnack(Successful,this)", true);
4
  • I don't see any problem here. Your code is correct. Just invoke your function with proper text. Commented Dec 28, 2017 at 8:53
  • can you please check by code behind. May be there is a problem.. Thanks Commented Dec 28, 2017 at 9:09
  • Add single quote around your meesage. ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "showSnack('Successful',this)", true); Commented Dec 28, 2017 at 9:12
  • thanks it works. i should wrap string in ' '. Commented Dec 28, 2017 at 9:12

1 Answer 1

2

Something like this? Not sure if I understand..

var showSnack= function (id, text) {
    x = document.getElementById(id)
    x.innerHTML = text;
    x.className = "show";
    setTimeout(function () { 
        x.className = x.className.replace("show","text"); 
    }, 4000);
}
let variablesnacks = ['snickers', 'chips', 'coke', 'popcorn'];
showSnack('snackbar', variablesnacks[Math.floor(Math.random()*(variablesnacks.length))]);

https://jsfiddle.net/4w8et8cc/3/

Sign up to request clarification or add additional context in comments.

1 Comment

it works thanks. the problem was with my code behind.

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.