Jump to content

What is wrong with this?


heldenbrau

Recommended Posts

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

 

This is to change a + button to a _ button when it is clicked

Link to comment
Share on other sites

Try:

 

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

 

If you're gonna use single quotes inside single quotes you need to escape them. In this case it's just easier to use double quotes.

 

For future reference when posting code you should use [php] or [code] tags.

Link to comment
Share on other sites

Rewrite it as a function. You'll save a lot of headache, and the  code will be cleaner:

 

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

 

And on the HTML element your using to trigger this event, use onClick="toggleBg();"

 

-jm

Link to comment
Share on other sites

That shouldn't matter; you can pass $count as a variable:

 

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

 

and use onClick="toggleBg('plus{$count}')"

 

I don't know what your code looks like, so I can't help you with the escape sequences for the quotes, but that shouldn't be hard to figure out.

 

-jm

Link to comment
Share on other sites

Thanks for your help.

 

I have put it in as a function now, but it still isn't working the function comes out on the webpage source as toggleBg('plus33')

which is the right number, so it should work if 'plus33' goes in the brackets.  It is part of a bigger javascript, I will post that, but I can't post the whole thing, it is massive.  It is $clicker btw not $count, but I've changed that.  Here is the function and the onclick part.

 

<script type="text/javascript">
function toggleBg(){

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

 

onclick="
if (document.getElementById('rev$clicker').style.display=='block'){
  document.getElementById('rev$clicker').style.display='none';
  }
else {
  document.getElementById('rev$clicker').style.display='block';
}
if (document.getElementById('box$clicker').style.display=='block'){
  document.getElementById('box$clicker').style.display='none';
  }
else {
  document.getElementById('box$clicker').style.display='block';
}
if (document.getElementById('revb$clicker').style.display=='block'){
  document.getElementById('revb$clicker').style.display='none';
  }
else {
  document.getElementById('revb$clicker').style.display='block';
}
if (document.getElementById('boxb$clicker').style.display=='block'){
  document.getElementById('boxb$clicker').style.display='none';
  }
else {
  document.getElementById('boxb$clicker').style.display='block';
}
toggleBg('plus{$clicker}')
}
"/>

Link to comment
Share on other sites

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.