VijayD
VijayD

Reputation: 818

how to format popup text in java script?

I want to format popup text using java script. but unable to do so. I did as follow -

 `var str =  "Your changes won\'t be saved. Please press 'Cancel' to stay on the same page or 'OK' to continue.";
var strbold = str.bold()`
to show this msg i've done the following in the code 
       `       var retval=confirm(strbold);
               if(retval==true){
               isDirty = false;
               $(this).trigger( "click" );`

but in the output instead of msg in the bold it appears something like <b>Your changes won't be saved. Please press 'Cancel' to stay on the same page or 'OK' to continue.<\b>

so how to format the popup text? p.s. i want text in bold.

Upvotes: 0

Views: 641

Answers (1)

CerebralFart
CerebralFart

Reputation: 3490

the str.bold() method is not supported by every of the most common browsers, however you could use something like this

function popup(t){
    function destroy(){
        document.getElementsByClassName("rtPopup")[0].remove()
        document.getElementsByClassName("rtPopupBack")[0].remove()
    }
    var d1=document.createElement("div");
        d1.className="rtPopup"
        d1.innerHTML="<div class='rtPopupX'>X</div>"+t
    var d2=document.createElement("div");
        d2.className="rtPopupBack"
    document.getElementsByTagName("body")[0].appendChild(div1)
    document.getElementsByTagName("body")[0].appendChild(div2)
    var c=document.getElementsByClassName('rtPopupX')
    for(var i=0;i<c.length;i++){
        c[i].onclick=function(){destroy()}
    }
    document.onkeydown=function(e){
        if(e.keyCode==27){
            destroy()
            document.onkeydown=function(e){}
        }
    }
    return{
        destroy:function(){destroy()}
    }
}

It uses an extended DOM object, the important extension is the following

Element.prototype.remove=function(e){
    for(var i=e.length-1;i>=0;i--){
        e[i].parentNode.removeChild(e[i])
    }
}

It takes input as an HTML string and displays it as an alert, you'll need to add the buttons yourself. It does require some CSS

.rtPopup{
    position:fixed;
    top:12.5%;
    left:12.5%;
    height:75%;
    width:75%;
    z-index:1000;
    border-radius:4px;
    background-color:#F9F8F0;
    margin:4px;
}
.rtPopup *{
    margin:5px;
    margin-top:30px;
}
.rtPopupX{
    position:absolute;
    right:5px;
    top:-25px;
    -moz-user-select:none;
    -webkit-user-select:none;
    -o-user-select:none;
    -ms-user-select:none;
    user-select:none;
    cursor:pointer;
}
.rtPopupBack{
    position:fixed;
    top:0;
    left:0;
    width:100%;
    height:100%;
    background-color:black;
    opacity:0.5;
    z-index:999;
}

It is from an API I'm working on, so feel free to use it where ever you want.

Upvotes: 1

Related Questions