views:

1392

answers:

4

Hi,

I have the html given below:

<ul id="thumbsPhotos">
     <li src="/images/1alvaston-hall-relaxing-lg.jpg"  onclick="updatePhoto (this.title)"><img src="/images/1alvaston-hall-relaxing-sl.jpg"  width="56" height="56"></li>
     <li onclick="updatePhoto(this.title)" src=""><img src="" width="56"  height="56"></li>
     <li onclick="updatePhoto(this.title)" src=""><img src="" width="56"  height="56"></li>
</ul>

Now, I want to replace all the "src" in <li> tags not in <img> tags using InnerHTML. I mean my output will be this:

<ul id="thumbsPhotos">
     <li title="/images/1alvaston-hall-relaxing-lg.jpg"  onclick="updatePhoto(this.title)"><img src="/images/1alvaston-hall-relaxing-sl.jpg"  width="56" height="56"></li>
     <li onclick="updatePhoto(this.title)" title=""><img src="" width="56" height="56"></li>
     <li onclick="updatePhoto(this.title)" title=""><img src="" width="56" height="56"></li>
</ul>
A: 

You could most likely use a RegEx replace that matches on the src attribute and do the conversion. I see that you want to also add the title attribute even if there is no match, that should be possible as well.

I'll try to dig up some RegEx examples for this.

Mitchel Sellers
+2  A: 

Not an answer, but that is not a very semantic way to do things. Where is this coming from? Why is there ever a src on the li element?

Assuming you want a javascript option, you could use something like jquery to do:

$(document).ready(function() {
    $("li[src], li[src='']").each(function() {
        var li = $(this);
        li.attr("title", li.attr("src"));
        li.removeAttr("src");
    });
});
Chris Marasti-Georg
can it be possible with using innerHTML, using regex
MKS
In the browser? You still need to run some sort of javascript. Why are you closing yourself off to this solution? Is this a homework problem?
Chris Marasti-Georg
basically I need to change this "src" to "title" on page run time
MKS
Well, the code I have there will do that. You can host the jquery library, or link to the minified version at http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js.Then just throw the code above into a script tag.
Chris Marasti-Georg
And you still haven't answered the question of *why* you have a src attribute on the li tag? Why not create correct HTML in the first place?
Chris Marasti-Georg
heh - my answer using plain javascript is actually less code. :)
nickf
Yes it is, and it should probably be above mine :) Assuming the guy knows how to accept an answer, it will be.
Chris Marasti-Georg
+3  A: 

Not tested, but here's a regex which might do it for you...

// find:
<li ([^>]*)src="(.*?)"(.*?)>

// replace:
<li $1title="$2"$3>

Update: tested and it works on your example.

If you wanted to run this on the client side using Javascript (for whatever whacky reason), you could do this:

var ul = document.getElementById("thumbsPhotos");
ul.innerHTML = ul.innerHTML.replace(
    /<li ([^>]*)src="(.*?)"(.*?)>/g,
    '<li $1title="$2"$3>'
);
nickf
can you please give this solution using innerHTML
MKS
That _is_ innerHTML? o_O ??
MDCore
Thanks Nickf!! it worked for me!!
MKS
A: 

If you need to do this once wouldn't just find and replace work?

Otherwise I agree that a regex is the best option.

Andrew Cox