Solved

adding a href tag to a table cell with javascript

Posted on 2016-08-29
3
51 Views
Last Modified: 2016-08-30
i have this fiddle:

https://jsfiddle.net/tkwpztkf/

Is there a way to use either JavaScript or jQuery

So that on page load my page renders this:

        <table id="table1">
            <tr>
                <td>
                    <span><a href="http://www.google.com" target="_blank">Google<a></span>
                </td>
                <td>
                    <span><a href="http://www.yahoo.com" target="_blank">Yahoo<a></span>
                </td>
                <td>
                    <span><a href="http://www.bing.com" target="_blank">Bing<a></span>
                </td>
            </tr>
        </table>

Open in new window


Basically I want to add the hyperlink a href tag to my items inside the cells on the jquery page load event.
0
Comment
Question by:maqskywalker
3 Comments
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 250 total points
ID: 41775443
What criteria would you use on the <span> to figure out what href to add?

Here is some sample code that does what you want but using hard coded rules
HTML
    <table id="table1">
      <tr>
        <td>
          <span>Google</span>
        </td>
        <td>
          <span>Yahoo</span>
        </td>
        <td>
          <span>Bing</span>
        </td>
      </tr>
    </table>

Open in new window

JQuery
$(function() {
  $('#table1 td span').each(function() {
    var text = $(this).text();
    $(this).html(
      $('<a/>')
        .html(text)
        .attr('href','http://www.' + text + '.com')
      );
  });
});

Open in new window

0
 
LVL 1

Assisted Solution

by:AnthonyD3
AnthonyD3 earned 250 total points
ID: 41775601
I would do it this way:

<table>
  <tbody>
    <tr>
      <td data-href="https://www.w3.org/">W3C</td>
      <td>No Link</td>
    </tr>
  </tbody>
</table>

Open in new window


(function($) {
  $(document).ready(function() {
    // Loop through all table cells
    $('td').each(function(i) {
      // Attempt to store the href in a variable
      var redirect = $(this).attr('data-href');
      // If the attribute exists...
      if (typeof redirect !== typeof undefined && redirect !== false) {
        // Bind the click event to this cell
        $(this).click(function() {
          // Redirect when the user clicks
          window.location = redirect;
        });
      }
    });
  });
})(jQuery);

Open in new window

0
 
LVL 1

Author Closing Comment

by:maqskywalker
ID: 41776856
Thanks guys. very cool.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MVC5 - Two different JQuery libraries are loading???? 2 21
maps stopped work unsure why 7 34
Get the number of current month days 2 23
JavaScript let vs var 5 21
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question