1

I have to set margin dynamically to an element. Earlier it was done by like following.

var starLink = star.createChild({
            tag: 'a',
            html: this.values[i],
            href: 'javascript:void',           
            title: this.showTitles ? this.titles[i] : ''
        });

        // Prepare division settings
        if(this.split) {
          var odd = (i % this.split);              
          star.setWidth(sw);
          starLink.setStyle('margin-left', '-' + (odd * sw) + 'px');          
        }

But now I changed my code as below. How will I set the margin here?

var starLink = document.createElement('a');        
        starLink.href = 'javascript:void';         
        starLink.title = this.showTitles ? this.titles[i] : '';
        starLink.innerHTML = this.values[i];
        star.appendChild(starLink);

 // Prepare division settings
        if(this.split) {
          var odd = (i % this.split);              
          star.setWidth(sw);
          starLink.setStyle('margin-left', '-' + (odd * sw) + 'px');              
        }
2
  • 1
    element.style.margin or maybe you should use setAttribute method? Commented Dec 18, 2012 at 6:40
  • I tried. But dont find the correct method. Could you please give me a sample code? Commented Dec 18, 2012 at 6:41

2 Answers 2

2

starLink.style.marginLeft='10px' or starLink.style.margin = "0px 0px 0px " + xOffset + "px";

Sign up to request clarification or add additional context in comments.

Comments

0

How about

var starLink = document.createElement('a');        
starLink.href = 'javascript:void';         
starLink.title = this.showTitles ? this.titles[i] : '';
starLink.innerHTML = this.values[i];
star.appendChild(starLink);

// Prepare division settings
if(this.split) {
    var odd = (i % this.split);              
    star.setWidth(sw);
    starLink.style.marginLeft = '-' + (odd * sw) + 'px';              
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.