-1

I want to call a function from a file using href. Below is my javascript code:

 function createtable() {
   function displaytable(argument) {
     var tr=document.createElement('tr');
     for (var i=0; i < argument.length; i++) {
       var td=document.createElement('td');
       if (i == 0) {
         td.appendChild(document.createTextNode(argument[i]));
       }
       if (i == 1) {
         td.appendChild(document.createTextNode(argument[i]));
       }
       if (i == 3) {
         td.appendChild(document.createTextNode(argument[i]));
       }
       if (i == 2) {
         td.ondblclick= function () {
         var column_index= (this.cellIndex) -1;
         var row_index= (this.parentNode.rowIndex);
         search_cell(row_index,column_index);
       }
       td.appendChild(document.createTextNode(argument[i]));
     }
     tr.appendChild(td);
    }
    document.getElementById('table_body').appendChild(tr);
  }
  document.write("<table border=\"1\"><tr><th>INDEX</th><th>--CELL NAME--</th><th>--PIN NAME--</th><th>--PG PIN--</th></tr><tbody id='table_body'></tbody></table>");

 for (var x=0; x < array_cells.length; x++) {
  displaytable([x+1,array_cells[x].cell,array_cells[x].pins,array_cells[x].pg_pins])
 }
 function search_cell(row_index,column_index) {

//Want to call this function using href } }

I want to add a functionality i my code that when i click over a cell in my html table it calls a function from file using href.

sam1989
  • 7
  • 5
  • 1
    Have you tried something that isn't working? There's no reason why you can't call a JavaScript function on the click event of any element on the page. – David Jul 20 '12 at 12:18

2 Answers2

0

Your question is not clear. But this is what I understand you want:

<table>
    <tr>
        <td>Column 1</td>
        <td>Column 2</td>
        <td>Column 3</td>
        <td><a href="#" id="link1">Function 1</a></td>
    </tr>
</table>

For the JS part:

<script type="text/javascript">

    function doSomething( data ) {
        alert(data + ' clicked');
    }

    document.getElementById('link1').onclick = function () {
        doSomething( this.innerHTML );

        return false;
    }

</script>
M. Ahmad Zafar
  • 4,881
  • 4
  • 32
  • 44
  • If this was the answer you were looking for then mark this answer as correct – M. Ahmad Zafar Jul 20 '12 at 12:39
  • sir i want to do this using javascript....tell me how can i do this in javascript...so that i write code in javascript file only and source it in html file – sam1989 Jul 20 '12 at 12:43
  • As @David has mentioned, if you have mentioned JS references than it shouldn't be a problem **** – M. Ahmad Zafar Jul 20 '12 at 12:51
  • Give your – M. Ahmad Zafar Jul 20 '12 at 13:10
  • @sam1989: What on Earth are you doing here?: `` That code inside the `script` tag makes no sense in that context. It's probably throwing errors to your JavaScript console. Additionally, you're making it _very_ difficult to help you. It really sounds like what you need is to follow some tutorials and practice and, well, learn JavaScript. We can't do it all for you. – David Jul 20 '12 at 13:11
  • You don't have to add id attribute to HTML tags in JavaScript. As @David said, you need to go through tutorials so I am sharing these links with you: http://www.w3schools.com/js/js_functions.asp and http://www.w3schools.com/js/js_events.asp – M. Ahmad Zafar Jul 20 '12 at 14:56
0

In response to your comment, mostly...

I've never seen the use of link.setAttribute() to accomplish this. So I don't even know if that's a workable approach. But you definitely don't want to use href to execute a JavaScript function. Nor do you want to reference the JavaScript file. In the scope of the JavaScript code and other things loaded into the DOM, the file is irrelevant. Once the JavaScript code is loaded, it's available throughout the DOM.

So the first thing you'll want to do is load the JavaScript (probably in the header of the HTML, but anywhere before it's used really):

<script type="text/javascript" src="path/to/file.js"></script>

(Note that this is not a self-closing tag. It needs to use the full closing syntax per the HTML spec.)

Once the JavaScript code is loaded, you can call any function within it from any click event. So if, in your code, link is a value referencing the targeted DOM element, then you'd want the onclick of link to call the function:

link.onclick = someFunction();

There are more examples here.

Community
  • 1
  • 1
David
  • 208,112
  • 36
  • 198
  • 279
  • sir can you provide me full code how to make my contents of table possible to call a function because i m totally confused – sam1989 Jul 20 '12 at 12:48
  • @sam1989: I don't know what the "contents of your table" are, so no I can't. You can, however, edit your question to include more code and more examples of what you're trying to do. In general, what you're trying to do is bind a JavaScript function call to the `onclick` event of one or more HTML elements. There are multiple ways to do this. In what way are you confused? What don't you understand about it? – David Jul 20 '12 at 12:51
  • @sam1989: The part in that code which catches my attention is: `link.onclick=sample();` Is that successfully calling the `sample()` function for that element's `onclick` event? If not, is there an error? Also, what is `document.createElement('link')`? I'm not familiar with a `link` element in HTML. Is that a new HTML5 element? A custom element? Or did you mean to use an `a` element instead? – David Jul 20 '12 at 13:03
  • @sam1989: First of all, you need to stop dumping unformatted code into comments. It's very difficult to read. If you have clarification to add to your question then add it to the question as an edit, not as a comment. And definitely not as a comment on each individual answer. Also, stop demanding that everybody provide you with complete code. You need to learn some basic JavaScript. We can help you with specific issues, but we're not going to write your code for you or give you full JavaScript tutorials. As it stands, your problem makes no sense. Clarify in your question. – David Jul 20 '12 at 13:15
  • can you tell me some reference books and pdfs from where i can learn javascript better.... – sam1989 Jul 20 '12 at 13:16
  • @sam1989: John Resig is a leading resource on JavaScript expertise, his books would be good: http://ejohn.org/ Just Googling the term "JavaScript books" shows some more results. Googling "JavaScript tutorials" also returns a lot of results. – David Jul 20 '12 at 13:18