Zia UsafXai
Zia UsafXai

Reputation: 73

Pass String as Parameter to InnerHtml attribute of object in Javascript

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);

Upvotes: 0

Views: 594

Answers (1)

simon
simon

Reputation: 944

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/

Upvotes: 2

Related Questions