3

How do you just change the text "us-test" from the value of the link below, without having to replace the whole link, the current method i use replaces the whole link i guess that has to do with the function of .attr, i'm guessing i could achieve this by using .find or something else i really don't have any clue on how to do it can someone help me on this thank's.

$(document).ready(function(){ 
 $("#box1").click(function(){
 $("a.mylink").attr("href", "http://google.com");
   });  

 $("#box2").click(function(){
  $("a.mylinktwo").attr("href", "http://yahoo.com"); 
 });   


<a class="mylink" href="http://google.com/en/get.php?chrome=us-test">Test</a>
2
  • You are doing it right by replacing the entire attribute. Commented Feb 17, 2012 at 6:42
  • so i guess i can't replace just the specific text i want to, im asking this because im gonna be using a bunch of links and i rather change the text that way it will be less stuff on the file... it will look a lot more cleaner than having 20 long links. Commented Feb 17, 2012 at 6:48

4 Answers 4

16

It depends on what you're trying to achieve. I wouldn't suggest complicating things too much, and if you can know before hand what URL to go to, replacing the whole URL would probably be best, and easiest to see what's going on.

To replace parts of a string, the replace() method would help.

var url = $('.mylink').attr('href')
url = url.replace('us-test', 'replaced-text')
$('.mylink').attr('href', url)

You can also pass a regular expression as the matching argument.

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

2 Comments

Thanks Rob just what i was looking for, one question tho what does "$('.mylink').attr('href', url)" do :b
that sets the href attribute for the link that you are trying to update.
2

Do you mean:


$(".mylink").click(function (e) {
  e.preventDefault();
  var href = $(this).attr("href").replace(/us-test/, "");
  //or replace with something
  var newHref = $(this).attr("href").replace(/us-test/, "somethingelse");
  $(this).attr("href", newHref);  
});

1 Comment

ummm... i try this and i cant seem to get it working, i guess the problem is on my end, thanks anyways Sudhir...
1

Use this Code:

$(document).ready(function() {
 var link = $('a').attr('href');
var equalPosition = link.indexOf('=');

var sptext= link.substring(equalPosition + 1);

 var s=link.replace(sptext,"replaced-text");

  $('.mylink').attr('href', s);




});

check this Fiddle http://jsfiddle.net/W3pmu/1/ Hope it helps...

Comments

0

To replace parts of a string, used replace() method .

jQuery(".mylink").on('click', function() {
  var url =  jQuery(this).attr('href')
  url = url.replace('us-test', 'replaced-text')
  jQuery(this).attr('href', url) 
 });

1 Comment

if your attribute in loops try once

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.