Languages

Menu
Sites
Language
Jquery function to compare background image of button when button click

Hi,

I want to comape background image of button when button click,my code is

*********************************************************

js file

$(document).ready(function() {

    $("#mt1").click(function() {
        if (document.getElementById("mt1").style.backgroundImage === "url('images/cmst1.png')")
            {
            
            alert("hello world");
            }
        
    });

******************************************************

css file

.imghr
{
    background:transparent;    
    background-image:url('images/cmst1.png');

    height:20px;
    width:120px;
    background-repeat: no-repeat;    
    border:none;
    outline:none;
    display:block;
   margin-top:10px;
   text-align:center;
}

**************************************

Html file

<button type="button" id="mt1" class="imghr"></button>

 

*******************************************

my code does not give me what i want,please help me as soon as possible.

 

thanks in advance

Thanks and regards

Mohit Kumar

 

 

 

Responses

6 Replies
Vikram

Hello,

 It  works  I add this code in initialization. but it will meet  your intend.

    document.getElementById("mt1").style.backgroundimage = "url('/images/cmst1.png')";

mohit kumar

Hi,

It work well when i assign image in button background,but when i compare it in if statement then nothing happend.

I want it initialization by css file

document.getElementById("mt1").style.backgroundimage = "url('/images/cmst1.png')";

if( document.getElementById("mt1").style.backgroundimage ==== "url('/images/cmst1.png')";)

{

alert("hello world");

}

 

its not work

please help me

 

Mohit Kumar

Vikram

Hello,

How about you try this code? you can check the url of image.

 

   document.getElementById("mt1").style.backgroundimage = "url('/images/cmst1.png')";

      alert(document.getElementById("mt1").style.backgroundimage) ;

   if( document.getElementById("mt1").style.backgroundimage === "url('/images/cmst1.png')";)

       {

          alert("hello world");

        }

mohit kumar

Hi,

It work fine.Thank you.

 

TIZEN is no 1 mobile OS

 

 

 

 

Thanks and regards

Mohit Kumar

Palitsyna

Hi,

you can use this code to get image url:

$("#mt1").click(function() {
	var bg = $("#mt1").css('background-image');
	alert(bg);
});

and use it to compare in 'If' statement.

 

 

mohit kumar

Hi,

It work fine.Thank you.

 

TIZEN is no 1 mobile OS.

 

 

 

 

 

Thanks and regards

Mohit Kumar