Verlon Marsh
Verlon Marsh

Reputation: 45

jquery get url from data and replace as href

I need some help with a javascript function. I have this script

$.get('http://ste.com/a-link.php', function(data) {
  alert(data);
});

where "data" contains a link wich i need to use it as "a href".

For example,

<a href="#">link</a>

When I click "link" the # from "a href" to be replaced with the url from "data" Maybe someone get the idea and help me with this. I am a noob with javascript.

Upvotes: 1

Views: 422

Answers (1)

iAmClownShoe
iAmClownShoe

Reputation: 616

you should use jquery for this as it will provide with you built in methods that make things like this easier.

you would call this in your javascript most likely on document ready. what was mentioned about the format of the data the is returned is valid. Most likely the script you will be calling will return data to you in json format which you will have to handle with javascript. This is easy using dot notation but required nonetheless. You would access it something like data.url

   $.get('http://ste.com/a-link.php', 
        function(data) {
           $('a').on("click", function(){
                  $(this).prop('href', data);     
               });
       });

Upvotes: 1

Related Questions