0

<a> tags are on a table.

--HTML--

<tbody id="TableBody">
<tr>
    <td class="span2"><span class="line"></span><b><a href="home/nextpage">1207012097</a></b></td>
</tr>
<tr>
    <td class="span2"><span class="line"></span><b><a href="home/nextpage">1207011881</a></b></td>
</tr>   
<tr>
    <td class="span2"><span class="line"></span><b><a href="home/nextpage">1207011857</a></b></td>
</tr>
</tbody>

What I want to do is view the text value of the clicked < a > tag (example: 1207012097) to the href target page which is "home/nextpage". Can you show me how to do this using JavaScript? TIA

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
LYKS
  • 69
  • 4
  • 13
  • 1
    what about to pass the value in the href call? like 1207012097 – Oriol Terradas Aug 01 '14 at 10:06
  • You can either store the value in a cookie, or pass it to the URL in the form of a hash (#...), or a query string (?...). You can access both the [hash](http://stackoverflow.com/questions/298503/how-can-you-check-for-a-hash-in-a-url-using-javascript) and the [query string](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) using JS. – Terry Aug 01 '14 at 10:07
  • `localStorage` is also an option. – GuyT Aug 01 '14 at 10:42

2 Answers2

1

You can catch the click on the link and add a query string parameter to the link:

$('#TableBody a').click(function(e){
  e.preventDefault();
  window.location.href = $(this).attr('href') + '?code=' + escapeURIComponent($(this).text());
});

In the next page you can get the value from the query string:

function querystring(key) {
  var re=new RegExp('(?:\\?|&)'+key+'=(.*?)(?=&|$)','gi');
  var r=[], m;
  while ((m=re.exec(document.location.search)) != null) r.push(m[1]);
  return r;
}

var code = querystring['code'][0];
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

IF you wouldnt want to passed the value via url parameters you might want to consider passing it by instantiation of form when you click a certain <a>


First lets change the structure of your <a>

<a href="home/nextpage">1207012097</a>

to

<a class='clickable_post' href="#">1207012097</a>


Embed JQuery Lib

<script type='text/javascript' src='jquery-1.10.2.js'></script>

Then the scripts would be

$(function(){

    $('.clickable_post').click(function(){
        var POST_VALUE = $(this).text();

      $('<form action="home/nextpage" method="POST">' + 
        '<input type="hidden" name="value" value="' + POST_VALUE + '">' +
        '</form>').submit();

    });

});

So what it literally does it

  1. trigger a click event when a classname with '.clikable_post' is hit
  2. store textValue of click <a> into a variable
  3. Create a form within the script with the selected input value before submiting to your previoously set href but now as an $_POST action
  4. Finally results are show on the script form page action and can be access via $_POST

cheers! LYKAPIGS.

Jhonathan H.
  • 2,734
  • 1
  • 19
  • 28