html - retrieve background image from css using javascript without using it's ID -
i've looked around , it's been hard find thread want do. far concerned, don't know if it's possible. looking retrieve background image file name (especially if link) when click. have script logs click last piece need background-image name (file-path name do) stored in css file. have idea or solution how can done without using div or class? here's have right now:
javascript & html
<script type="text/javascript"> var arraywithelements = new array(); //,replaytimer; document.onclick = clicklistener; function clicklistener(e) { var clickedelement=(window.event) ? window.event.srcelement : e.target, tags=document.getelementsbytagname(clickedelement.tagname); for(var i=0;i<tags.length;++i) { if(tags[i]==clickedelement) { if(clickedelement.tagname=="a") { arraywithelements.push({tag:clickedelement.tagname,index:i}); console.log(clickedelement.baseuri,clickedelement.href,clickedelement.innertext,document.location.href,document.images.href); } if(clickedelement.tagname=="img") { arraywithelements.push({tag:clickedelement.tagname,index:i}); console.log(clickedelement.baseuri,clickedelement.parentnode.href,clickedelement.innertext,document.location.href,document.getelementsbyid(element).src); } if(clickedelement.tagname=="div") { arraywithelements.push({tag:clickedelement.tagname,index:i}); console.log(clickedelement.baseuri,clickedelement.parentnode.href,clickedelement.innertext,document.location.href,document.getelementsbyid(element).src); } if(clickedelement.tagname=="class") { arraywithelements.push({tag:clickedelement.tagname,index:i}); console.log(clickedelement.baseuri,clickedelement.parentnode.href,clickedelement.innertext,document.location.href,document.getelementsbyid(element).src); } } } } </script> </head> <a id="1" href="#">trial1</a> <a id="2" href="http://www.google.com" target="blank">google</a> <a id="3" href="http://www.google.com">google</a> <a id="4" href="http://www.google.com" target="_blank"><img id="image" src="untitled.jpg"/></a> <a id="5" href="trial.html"> <input type="text" id="text-test"/> <a href="http://www.google.com"><div id="image-link"></div></a> css:
#image-link { background-image:url('untitled.jpg'); width: 50px; height:50px; border: 1px solid #000000; } this test file converted use in near future. thanks
on newer browsers, can use window.getcomputedstyle(clickedelement) find background image property.
Comments
Post a Comment