Jump to content

2 problems, why won't alert box work and why won't image change?


Recommended Posts

<script type="text/javascript">
function toggleBg(plus) {
if (document.getElementById(plus).style.background=="url('/images/plus.png')"{
  document.getElementById(plus).style.background="url('/images/minus.png')";
}else{
  document.getElementById(plus).style.background="url('/images/plus.png')";
}
}
alert("This is a test alert message");
</script>

 

I wanted to change the image of a button when it is clicked from a plus to a minus and then back to a minus.  The problem is, it won't change when clicked and if I put minus in the the else bit, it changes to a minus.  "url('/images/plus.png')" doesn't seem to match the actual property, so I wanted to get a printout of the actual property using an alert box.  But the alert box won't even work when it is in the presence of that function.  If I delete the function from the script area, the alert box works.  Why doesn't the alert box work in the script above?

JavaScript is event-driven, so any calls to functions or code have to be triggered by an event. If you place the alert code within a function that is called by the 'onload' event, it'll run no problems:

 

window.onload = function() {
    alert("This is a test alert message");
}

 

Also you'll most likely need to use .style.backgroundImage to return the URL.

How would I do that so that I could print out the property of document.getElementById(plus).style.background in the alert message.  The function togglebg is called when somebody clicks a button, I want the alert to tell me what  the background style is.  If I put the alert box code inside the fuction togglebg, nothing happens, but togglebg is triggered by an event.

<script type="text/javascript">
function toggleBg(plus) {
  alert("This is a test alert message");
if (document.getElementById(plus).style.background=="url('/images/plus.png')"{
  document.getElementById(plus).style.background="url('/images/minus.png')";
}else{
  document.getElementById(plus).style.background="url('/images/plus.png')";
}
}

</script>

.. And the alert is never shown? That suggests the function is never actually being called; which after looking appears to be a syntax error. You're missing a closing bracket:

 

if (document.getElementById(plus).style.background=="url('/images/plus.png')"){

I have solved it now, the bit where it says:

if (document.getElementById(plus).style.background=="url('/images/plus.png')"{
  document.getElementById(plus).style.background="url('/images/minus.png')";
}

 

Should be

 

if (document.getElementById(plus).style.background=='url("/images/minus.png") repeat scroll 0% 0% transparent'){
  document.getElementById(plus).style.background="url('/images/plus.png')";

 

I was able to see this by printing out the properties of the button with the alert.

ok, thanks for the tip, I've changed it to use backgroundImage and match the url only and it works in Firefox and Ie, just seen that the rest of the page is all over the place in IE though :( so more work to be done.  Thanks for getting this to work.

 

 

Spoke too soon.  In IE the plus changes to a minus, but doesn't change back into a plus again when I click it a second time.  Here is the script:

 

<style type='text/css'>@import url(styles/addcat.css);</style>
<script type="text/javascript">
function toggleBg(plus) {
if (document.getElementById(plus).style.backgroundImage=='url("/images/minus.png")'){
  document.getElementById(plus).style.backgroundImage="url('/images/plus.png')";
}else{
  document.getElementById(plus).style.backgroundImage="url('/images/minus.png')";
}
}

</script>

Try this:

 

function toggleBg(obj_id) {
    var obj = document.getElementById(obj_id);
    if (obj.style.backgroundImage.indexOf('minus.png') != -1) {
        obj.style.backgroundImage="url('/images/plus.png')";
    } else {
        obj.style.backgroundImage="url('/images/minus.png')";
    }
}

It didn't work before because the value of the .style.background property varies between browsers; so you can't do a straight comparison. The indexOf() method looks for the "minus.png" string within the backgroundImage property. If it's found you know the current image is the minus, so you can set it to the plus - else set it to the minus. Creating a variable for the object just makes it easier to read.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.