3

My html:

<span data-sample="http://mysite.com"></span>

My JS:

 var objLink = $(this).attr('data-sample');

Hence my objLink == "http://mysite.com"

How can I add "www" and make the objLink == "http://www.mysite.com"?

1 Answer 1

2

Not sure this is the answer you are looking for, but you can use .replace() to do this.

objLink = objLink.replace("http://", "http://www.");

Check the demo: http://jsfiddle.net/XZKaj/

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

3 Comments

And if you want to apply it back to the span, use $(this).attr('data-sample', objLink);
If you satisfied with the answer you will need to accept it. Please read the FAQ
Or .replace("://", "://www.") if you want to allow for potential https links. Maybe add a condition to only do the replace if the url doesn't already have "www" in it...

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.