-1

I'm new to JavaScript so excuse me for this question,, when i use jQuery to append data from firebase to a table

i want to append a button but has a href url from variable

url_val = is a variable url i want when i click to the button go to website


  $("#data").append("<tr><td>" + title_val + "</td><td><button class='box'> " + url_val + "</button></td></tr>");

i was trying to do the fowling

  $("#data").append("<tr><td>" + title_val + "</td><td><a href="url_val"> <button class='box'> " + GO + "</button></a></td></tr>");

but i cant add a variable inside is there a solution for this

A clear
  • 27
  • 5

1 Answers1

0

I think you've made two mistakes, the first is not using + to concantate strings appropriately as mentioned by @Taplar and @abney317.

Secondly you have unnecessarily taken 'GO' out of your hard coded string, but also not stored it as a variable or concatenated it appropriately.

I have fixed both of these and provided a demonstration.

Let me know if you were hoping for something else.


Demo

// Add click event to add row
$("#addRow").click(function() {

  // Store variables
  title_val = "Title";
  url_val = "www.google.com";
  
  // Append data
  $("#data").append("<tr><td>" + title_val + "</td><td><a href=" + url_val + "> <button class='box'>GO</button></a></td></tr>");

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table id="data">
</table>

<button id="addRow">Add Row</button>
Oliver Trampleasure
  • 3,293
  • 1
  • 10
  • 25