Need some javascript help

carepicha

...
Nov 11, 2010
25
0
0
Costa Rica
Code:
<script>
function clickPost(el){
    var im=el.parentNode.style.backgroundImage;
    var sp0=im.split("/");
    var sp1=sp0[sp0.length-1].split(".");
    document.getElementById("formInp").value=sp1[0];
    document.F.submit();
}
</script>

Code:
<div class="thumb" style="background-image: url('images/1.jpg');"><a href='javascript:void(0)' onclick='clickPost(this)'>

This code snags the number from the background-image file, I changed my code around to use css image overlays, before it was done in javascript and I believe I need to go to a childnode now but am having issues figuring it out.

Code:
<div id="container">
<a href='javascript:void(0)' onclick='clickPost(this)'><div class="overlay"></div>
<div class="image"><img src="images/2.jpg" /></a></div>
</div>

Thanks
 


First of all, you shouldn't have divs inside of a link... the former are block-level elements and links are inline. That could be messing up the JS engine too. Use span instead of div.

Then, try changing your function to:

Code:
function clickPost(el){
    var kids = el.childNodes;
    for(i=0; i<=kids.length-1; i++) {
        if(kids[i].nodeName == 'IMG') {
            var im = kids[i].getAttribute('src');
            break;
        }
    }
    if(im) {
        var sp0=im.split("/");
        var sp1=sp0[sp0.length-1].split(".");
        document.getElementById("formInp").value=sp1[0];
        document.F.submit();
    }
}

Not tested but this is the general priciple.