views:

75

answers:

5
$(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&lt;/a&gt;&lt;/p&gt;
A: 

Try:

$("a").each(function () {
    var $this = $(this);
    $this.attr('href', $this.attr('title'));
});
Andreas Grech
Oooh, that gets the embedding part to work, but the links' href's still aren't being changed to the title?
James Sampson
It's $('a') not $('href') on the first line. The selector is for the tag, while href is just an attribute.
Tudorizer
Have replaced it with a and still only doing the embedding part?
James Sampson
That part should be inside $(document).ready() too, BTW
Tudorizer
Yep, this is my full string now: http://pastebin.com/pD5kd4KM
James Sampson
If you give me some HTML, we can debug it on jsfiddle.net
Tudorizer
Actually, you probably need more than I gave you. On http://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.
James Sampson
+3  A: 

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.

BrunoLM
This has been settled. Someone should edit the question.
Tudorizer
@Tudorizer - Edit the question? Why?
patrick dw
Because that part was solved by the other answers.
Tudorizer
A: 

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');
});
Hooray Im Helping
None of the javascript works with this one unfortunately?
James Sampson
+1  A: 
patrick dw
As with the others, the embed works but not the title change? :(
James Sampson
@James - In your HTML, your image ends with `.jpeg` instead of `.jpg`. Check out this example which uses my most recent update: http://jsfiddle.net/5ZBVf/1/
patrick dw
@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)
patrick dw
This actually works better, thanks Patrick. Means it only actually changes the links I want. Thank you everyone!
James Sampson
@James - You're welcome. :o)
patrick dw
A: 

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.

Tudorizer
Thank you Tudorizer!
James Sampson
You're welcome.
Tudorizer
@Tudorizer: You can tell jsfiddle, to not wrap your code by setting the first dropdown box value in “Choose Framework” to “No wrap”.
poke
You don't need `$('body').append(img);`. And if `a` doesn't have `title` it will break the `href`.
BrunoLM
You're right, I've added that for jsfiddle. My bad.
Tudorizer
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!
James Sampson
I have edited the code on jsfiddle at http://jsfiddle.net/TbMzD/3/ for posterity.
Tudorizer
The source won't change, unless you're viewing from Firebug, in the HTML tab.
Tudorizer
My bad, made a mistake! Thanks
James Sampson