Matej Posted October 12, 2014 Share Posted October 12, 2014 hello , i have script http://jsfiddle.net/hc4erouc/2/ why does prototype function does not work and breaks the loop? Link to comment Share on other sites More sharing options...
Ingolme Posted October 12, 2014 Share Posted October 12, 2014 Because you haven't defined the function until after calling it. Create the color() function before the for() loop. Link to comment Share on other sites More sharing options...
davej Posted October 12, 2014 Share Posted October 12, 2014 Perhaps... <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><title>title</title><style>.lol { border:1px solid black}</style><script>window.onerror = function(m, u, l){alert('Javascript Error: '+m+'nURL: '+u+'nLine Number: '+l);return true;}</script><script>window.onload = init;function init() {document.getElementById('btn1').onclick = test;}function little(x) { this.element =x; var matej=this.element.getAttribute("data-size"); if(matej!==null){ this.element.style.width = matej + "px"; this.element.style.height = matej + "px"; }}little.prototype.color = function() { this.element.style.backgroundColor="red";};function test(){var kek=document.getElementsByClassName("lol");var g=[];for(var i=0;i<kek.length;i++){ var l = new little(kek[i]); l.color(); g.push(l);}}//end text</script></head><body><input type="button" id="btn1" value="Enter"><div class="lol" data-size="100"></div><div class="lol" data-size="110"></div><div class="lol" data-size="120"></div><div class="lol" data-size="130"></div></body> </html> Looking at the subject line, I don't see any elements being created. I see an array of objects being created. Link to comment Share on other sites More sharing options...
Matej Posted October 12, 2014 Author Share Posted October 12, 2014 Thank you . Yea sorry , im not good at naming thins or subject :/ anyway , i tried to add a child div to one of them here http://jsfiddle.net/hc4erouc/3/ but whenever i set both this.element.style.width=parseInt(this.element.parentNode.style.width/2)+"px"this.element.style.height=parseInt(this.element.parentNode.style.height/2)+"px" it aint working , but when i add just this this.element.style.width=parseInt(this.element.parentNode.style.width/2)+"px" // this works strange , it add width of element but instead of 50% of parent's width it adds 100% of parent's widththis.element.style.height=20+"px" Link to comment Share on other sites More sharing options...
davej Posted October 12, 2014 Share Posted October 12, 2014 I have no idea what you are trying to do. https://developer.mozilla.org/en-US/docs/Web/API/CSS_Object_Model/Determining_the_dimensions_of_elements Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now