views:

34

answers:

1

I'm trying to do the following in jquery but can't make it work .

I want to select all the DIV tags that have a custom attribute called NODE_ID that have a value of 49_3. then select the and change the src attribute to a different image.

Here's the HTML:

<div style="display: block;" id="71_7_sub_p_div" node_id="49_3">
    <span>
        <img src="../images/maximize.png"> &nbsp;Greenfield Industrial Park
    </span>
</div>   

Here's the script:

<script type="text/javascript">
    $(document).ready(function(){
        $("div[node_id='49_3']:has(img)").attr("src","../images/folder_closed.png");
    });
</script>

Any ideas are appreciated. thanks.

+3  A: 
$("div[node_id='49_3'] img").attr("src","../images/folder_closed.png");

That should work but I would suggest that you use CSS for these images rather than use javascript to switch the src.

<div style="display: block;" id="71_7_sub_p_div" node_id="49_3" class="maximize">
    <span>&nbsp;Greenfield Industrial Park</span>
</div> 

CSS

.maximize{
    background: url(/images/maximize.png) no-repeat;
    padding-left: 10px; // width of icon
}
.minimize{
    background: url(/images/folder_closed.png) no-repeat;
    padding-left: 10px; // width of icon
}

Javascript:

<script type="text/javascript">
    $(document).ready(function(){
        $("div[node_id='49_3']").removeClass('maximize').addClass('minimize');
    });
</script>

The benefit of doing it like this is that you can easily manage the different states styles in the CSS and don't have to dig through the javascript when you need to change the url to the image. Also it gives you an easy way to figure out the state of the "folder".

PetersenDidIt
wow, not only were u fast at replying, but spot on with the code! Thank you so much! Works perfectly!.
Ronedog
@Ronedog updated the answer with a better way to handle this type of thing
PetersenDidIt