Jump to content

[Javascript] Simple click indentation question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
HumansAreFriendsNotFood

HumansAreFriendsNotFood

    Newbie

  • Members
  • PipPip
  • 24 posts
Hey guys - I'm having a little trouble... I want to make 4 images individually indent when clicked and I'm trying to use Jscript to achieve this.

So I've got this:

var Indent =

{

  init: function()

  {

    var foo = document.getElementsByTagName("img");

      for (i=0,ii=4; i<ii; i++) {

        foo[i].onclick = foo[i].style.setAttribute("marginLeft", "10px");

      }

  },

};


Indent.init();


The four images that I want to apply this effect to are the first 4 in the document so hopefully you can see my thinking here.

Any ideas as to why this isn't working?

Thanks in advance :)

#2
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
change this
foo[i].onclick = foo[i].style.setAttribute("marginLeft", "10px");
to
foo[i].onclick = foo[i].setAttribute("Style","margin-Left:10px");


#3
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
a better option would be jquery i suppose
http://stackoverflow.com/questions/95731/why-does-an-onclick-property-set-with-setattribute-fail-to-work-in-ie