varun kumar
varun kumar

Reputation: 63

create the variable link as a hyperlink javascript

create a hyperlink with the variable link

<html>
<body>
<center><h1> retrive data</h1></center>
<h1 id="head1"> </h1>
<input type="text" placeholder="enter your unique id" id="pass"/>
 <input type = "button" value = "submit" id="but" onclick="myfunction();"/>


<script>
   var pass;

function myfunction()
{
   pass = document.getElementById("pass").value;
  document.writeln(pass);
  document.writeln("<br>");
  document.writeln("<br>");
  document.writeln("<br>");
  document.writeln("<br>");
  var passwordToLookFor = pass;
var ref = firebase.database().ref("users");
var query = ref.orderByChild("password").equalTo(passwordToLookFor);
query.once("value").then(function(snapshot) {
  snapshot.forEach(function(child) { // loop over the results
    console.log(child.key);
    console.log(child.val().user_name);

    var link = child.val().user_name;
    document.writeln(link);
    });
});
}
</script>
</body></html>

i want to create the value of link as a hyperlink i want the hyperlink to be created once when the function is called

Upvotes: 0

Views: 76

Answers (3)

Kyle
Kyle

Reputation: 1503

Are you just looking for how to make it an anchor tag?

<script>
   var pass;

function myfunction()
{
   ...

    var link = child.val().user_name;
    document.writeln("<a href='"+link+"' target='_blank'>"+link+"</a>");
    });
});
}
</script>
</body></html>

Upvotes: 2

Chris
Chris

Reputation: 282

If I understand correctly and the link variable contains the actual address you want to navigate to, then this will work. First simply set an ID on the div you want to populate with links:

<div id="target-div"></div>

Then populate it like so (I just created an array for demo purposes, but this would be your snapshot.forEach:

var links = ['link1', 'link2', 'link3']

var targetDiv = document.getElementById("target-div");

links.forEach(function(link) {
    var anchor = document.createElement('a');
  anchor.href = link;
  anchor.innerText = link;
  targetDiv.appendChild(anchor);

  var br = document.createElement('br');
  targetDiv.appendChild(br);
});

Demo: https://jsfiddle.net/csnuh7rd/2/

Upvotes: 0

Dan Ruswick
Dan Ruswick

Reputation: 3170

You can create an a dom element like this:

let link_el = document.createElement('a')
link_el.href = link // assuming link holds the value of the href you want

Then insert it into the dom wherever you want.

Upvotes: 0

Related Questions