redrom
redrom

Reputation: 11642

Changing href attribute doesn't work in jQuery Mobile

I would like to change href using jQuery Mobile, I tried a some code examples like:

$("a[href='http://www.google.com/']").attr('href', 'http://www.live.com/');
<li><a data-ajax="false" href="http://www.google.com" >Navigate</a></li>

and

$("#address").append("href", "http://cupcream.com");
<li><a data-ajax="false" id="address" href="http://www.google.com" >Navigate</a></li>

But nothing happens.

What can be wrong, aren't this some bug in jQuery Mobile?

Upvotes: 0

Views: 4823

Answers (4)

lowcrawler
lowcrawler

Reputation: 7599

If you do not have the data-ajax="false" then the link will not be changeable via the jquery attr function. I see that you have it, but I'm mentioning it for future searches.

Once you have that, you can change the link like so: $('a[href='http://www.google.com/']').attr('href', 'http://www.live.com/');

I suggest ensuring that your selector is working as expected by doing a 'hide()' on a test case as well.

Upvotes: 1

Jura Khrapunov
Jura Khrapunov

Reputation: 1024

Use .attr() method, not .append()

$("#address").attr("href", "http://cupcream.com");

Upvotes: 0

d.b
d.b

Reputation: 105

Changing just href attibute did not work for me. I had to change both, text and the attribute href and that worked just fine. Tested on Chrome, Firefox and IE10.

$('#campuslink').text('http://www.google.com');

$('#campuslink').attr('href','http://www.google.com');

This fixed the issue for me.

Upvotes: 1

m.piunti
m.piunti

Reputation: 368

You need to add to your "a" element the attribute rel="external" or data-ajax="false", in order for the links to not be managed via Ajax. Official Documentation here.

Also look at JQuery Mobile History on data-ajax=false

Upvotes: 1

Related Questions