|
|
Code of the page :
I wish to search by the href(the only unique static identifier within this div, div id is randomized), to determine the div location, and through that, change the img src. ie. i only want to change images/img.gif to images/img2.gif when it is contained in the same div as member.php?u=1. at the moment i'm doing something along the lines of ////////////////////////////////////// var user_elements = document.getElementsByTagName('a'); for(var i=0;i<user_elements> ////////////////////////////////////// but I can't even find the right < a > tag. any advice? |
|
|
|
|
|
thank you. i was looking for something like user.previousSibling. however java console complains that user is undefined.
i changed those . to _, and then java console complained that a previousSibling was null, so i added another conditional. my code currently looks like this
Matched right member
and the image is not being updated. |
|
|
also, could you explain the last little bit of this line (user.href.match(/member\.php\?u=1/i) why are you (what looks to me) appending the iterator value to the end of the search string? |
|
|
dob, please stop posting non-working code. It's damn obvious that user.previousSibling is a text node, not to mention that images don't have href attribute.
var xp = document.evaluate('.//a[contains(@href,"member.php?u=1")]/preceding-sibling::img', document, null, 6, null), x;
for (var i = 0; x = xp.snapshotItem(i); i++) x.src = 'images/img2.gif';
|
|
|
thanks mikado, works like a charm. now if i wanted to take the text that < a >Text here< /a > is wrapped around (The 'Text here') portion and make it bold. how would i do that? I'm currently trying var yp = document.evaluate('.//a[contains(@href,"member.php?u=107270")]', document, null, 6, null), y;
to no effect. also, yp has something like 6 elements (at least for the page i'm working with), is it possible to isolate the one that has the img inthe preceding sibling? something like if(yp.preceding-sibling.type==img){
|
|
|
var xp = document.evaluate('.//img/following-sibling::a[contains(@href,"member.php?u=107270")]', document, null, 6, null), x;
for (var i = 0; x = xp.snapshotItem(i); i++) x.style.fontWeight = 'bold';To stop asking about every single line I recommend you to read the xpath reference and use DOM Inspector extension to discover available elements' properties. |
|
|
Wasn't so damn obvious to me. |