0
$(document).ready(function () {
    $("href").attr('href', 'title');
});

$('a[href$=.jpg]').each(function () {
    var imageSrc = $(this).attr('href');
    var img = $('<img />').attr('src', imageSrc).css('max-width', '300px').css('max-height', '200px').css('marginBottom', '10px').css('marginTop', '10px').attr('rel', 'lightbox');
    $(this).replaceWith(img);
});
});

This is the jQuery code I have at the moment, which I want to change all links' href to the same as their title, before then embedding them in the page. Yet with the changing href to title bit in the code, it stops working. I'm new to Javascript so am definitely doing something wrong, just not sure what yet! Any help much appreciated!

Thank you guys

EDIT

This is the html that I want to change:

<p class="entry-content">Some interesting content<a href="http://example.com/index.php/attachment/11" title="example.com/file/testing-20101016T114047-2k5g3ud.jpeg" rel="external" class="attachment" id="attachment-11">http://example.com/index.php/attachment/11</a></p>
1
  • Can you please post a bit of the HTML? It would help a ton. Commented Oct 16, 2010 at 14:30

5 Answers 5

3

You are changing it wrong, you are trying to select href elements instead of a.

This fix should do it:

$("a[title]").each(function() {
    $(this).attr('href',$(this).attr('title'));
});

It will select all a elements with title and set the href with this value.

Sign up to request clarification or add additional context in comments.

1 Comment

@Tudorizer - Edit the question? Why?
1

Here's a much more efficient way.

Since you're just replacing the <a> elements, there's really no need to change its href. Just select the <a> elements that end with jpg/jpeg, and use that attribute directly.

Example: http://jsfiddle.net/5ZBVf/4/

$(document).ready(function() {
    $("a[title$=.jpg],[title$=.jpeg]").replaceWith(function() {
            return $('<img />', {src:this.title, rel:'lightbox'})
                    .css({maxWidth: 300,maxHeight: 200,marginBottom: 10,marginTop: 10});
    });
});​

Your .each() is outside the .ready() function.

You can accomplish the href change easily like this:

$(document).ready(function () {
    $("a").attr('href', function() { return this.title; });
});

The .attr() method will accept a function where the return value is the new value of (in this case) href.

So the whole thing could look like this:

Example: http://jsfiddle.net/5ZBVf/3/

$(document).ready(function() {
    $("a[title]").attr('href', function() { return this.title; })
                 .filter('[href$=.jpg],[href$=.jpeg]')
                 .replaceWith(function() {
                     return $('<img />', {src:this.href, rel:'lightbox'})
                            .css({maxWidth: 300,maxHeight: 200,marginBottom: 10,marginTop: 10});
                 });
});

4 Comments

As with the others, the embed works but not the title change? :(
@James - In your HTML, your image ends with .jpeg instead of .jpg. Check out this example which uses my most recent update: jsfiddle.net/5ZBVf/1
@James - Updated this answer. More concise and efficient this way. Also covers both .jpg and .jpeg hrefs. Have a look and let me know if you have questions. :o)
This actually works better, thanks Patrick. Means it only actually changes the links I want. Thank you everyone!
0

This line:

$("href").attr('href','title');

Is finding all href elements and replacing their href attr with the string 'title'. Since there is no such thing as an href element, Try this instead:

// for every anchor element on the page, replace it's href attribute with it's title attribute
$('a').each(function() {
  $(this).attr('href', $(this).attr('title');
});

Comments

0

Check this out: http://jsfiddle.net/TbMzD/ Seems to do what you want. Note: $(document).ready() is commented because of jsfiddle, you actually need it in your code.

6 Comments

@Tudorizer: You can tell jsfiddle, to not wrap your code by setting the first dropdown box value in “Choose Framework” to “No wrap”.
You don't need $('body').append(img);. And if a doesn't have title it will break the href.
That seems to work, but when I view source, it isn't actually changed in the source, which seems to mean that the javascript won't embed it because it doesn't end in .jpg? When I click on it, it goes directly to the image, but isn't changed in the source? Thanks again Tudorizer!
I have edited the code on jsfiddle at jsfiddle.net/TbMzD/3 for posterity.
The source won't change, unless you're viewing from Firebug, in the HTML tab.
|
0

Try:

$("a").each(function () {
    var $this = $(this);
    $this.attr('href', $this.attr('title'));
});

5 Comments

Oooh, that gets the embedding part to work, but the links' href's still aren't being changed to the title?
It's $('a') not $('href') on the first line. The selector is for the tag, while href is just an attribute.
Have replaced it with a and still only doing the embedding part?
If you give me some HTML, we can debug it on jsfiddle.net
Actually, you probably need more than I gave you. On adchant.com/sn/statusnet-0.9.5/index.php is a test site where I'm trying to get it to work. Thank you so much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.