1

I would like to use jquery to update my attribute value, because I want the visitor when click the facebook likes then the likes' post will show different thumbnail in one html.

<link rel="img_src" href="myimg.jpg">

so I try to use the following code:-

$(document).ready(function(){
    $("link").attr("href", myLinkFromXML);
});

but seem like not work.

Do you guys have any idea on this?

Thanks for advance. :)

wyman
  • 279
  • 1
  • 3
  • 11

3 Answers3

3

There's no reason your code shouldn't have worked, except it would wipe out every stylesheet link tag href also

To target a specific one:

$('link[rel="img_src"]').attr('href', myLinkFromXML);

Now the issue is variable myLinkFromXML defined? I suspect from it's name you want this done in the success of an xml ajax call

charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

working demo try this: http://jsfiddle.net/69v27/

Good link: http://api.jquery.com/prop/

Also, Prop vs Attr - this is good read as well please see: .prop() vs .attr() (this link will elaborate on Prop api vs attr)

In case of single element that needs to be change I would suggest adding identity i.e. id attribute for the element and use like this $('#id_of_link').prop('href','to_whatever') or for using class in case of group of link $('.class_of_link').prop('href','to_whatever'). Or see comments below there is another way using your rel.

Please let me know if I missed anything,

Hope this helps

code

$(document).ready(function(){
    $("link").prop("href","mylinkfromxml");
    alert("New Link is ==> " + $("link").prop('href'));
});​
Community
  • 1
  • 1
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
  • you really want to change all the guy's stylesheets? – charlietfl Jun 19 '12 at 04:12
  • somehow "href" seems like an attribute to me rather than a property – nbrooks Jun 19 '12 at 04:13
  • @Joy yeah funny that, hmm oh and by the just in case for OP this is good link: http://stackoverflow.com/questions/5874652/prop-vs-attr – Tats_innit Jun 19 '12 at 04:14
  • @nbrooks please see here: http://stackoverflow.com/questions/5874652/prop-vs-attr good explaination – Tats_innit Jun 19 '12 at 04:14
  • @charlietfl yep Can do like this : `$('link[rel="img_src"]').prop` :). Nea, not all; as OP dint mention but yes can do this for specific but its mnot clear, for an individual he/she should use `id` or `class` for a group :) thanks for comment though, cheers – Tats_innit Jun 19 '12 at 04:16
0
  var newCSS = [];

   $.each(options.styles, function(index) {
    $('<link />', {
        href: '/styles/' + options.styles[index]['href'],
        media: 'screen',
        rel: 'stylesheet',
        type: 'text/css',
        'class': 'dynamic_css'
    }).appendTo('head');
  });
  // IE doesn't always notice the presence of new stylesheets
  if ($.browser.msie) {
       $('.dynamic_css').clone().appendTo('head');
  }

This code will add new css to document. Use this style and change code accordingly for your xml

Imdad
  • 5,942
  • 4
  • 33
  • 53