1

I've leraned js for a few days

as topic I want to put several hyperlinks in a var

var aaa = [
'www.google.com',
'www.yahoo.com',
'www.facebook.com'];

$('#showlink').text(aaa[2]);

and then "www.yahoo.com" link will show in html,it's an url

<div id="showlink"> </div>

how can i do for that?

1
  • in place of only URL you need to put complete anchor tag. Commented Apr 29, 2015 at 5:50

3 Answers 3

1

You could use the jQuery .html() function instead of .text() which allows you to dynamically update HTML, so;

$('#showlink').html('<a href="' + aaa[2] + '">' + aaa[2] + '</a>');

OR, you can append to the DIV with:

$('#showlink').append('<a href="' + aaa[2] + '">' + aaa[2] + '</a>');
Sign up to request clarification or add additional context in comments.

Comments

1

Here is the code

$("<a/>",{"href":"http://"+aaa[2],"text":aaa[2]}).appendTo("#showlink");

3 Comments

Please explain the OP that what does your code do and why OP need this code ;).
@shA.t this is simple. $('<a/>') creates a Anchor Node, with Text as the stored string in array. This also appends "http://" before URL string and makes that as href attribute. That anchor tag with text and href, then gets appended to "#showlink" div.
We are here to make a royal programming reference, thanks ;).
0

Add a button at your div like this

<div id="showlink"> </div>
<input type="button" id="gen" />

and use click() function in your js like this

$("#gen").click(function() {

   var aaa = [
'www.google.com',
'www.yahoo.com',
'www.facebook.com'];

 $("#showlink").val(aaa[2]);

});

Comments

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.