Solved

Using DOM to create text link that uses javascript

Posted on 2009-07-10
6
295 Views
Last Modified: 2013-11-19
I have a function that edits a row so that when the user clicks a text note link, the text note becomes a textbox with the text within it.  When the user clicks the new + button, another javascript function is called that does the opposite.  It takes the text in the textbox and makes it linked text.  
The original anchor tag for the link was like:  <a href="#" onclick="javascript:editNote(this.parentNode.parentNode.rowIndex)">.

Now, I'd like to have a similar link so the user can re-change the text he just edited.  Instead, when I click the link that's created from textbox, it just brings me to the top of the page.  Please look at the code and tell me what I'm doing wrong.

The code below shows just a single row in the table, but there may be multiple rows.

Thank you in advance,
Chris
<script language="javascript">
        function editNote(noteId, rowIndex) {
            var tblRef = document.getElementById("noteTbl");
            var row=tblRef.rows[rowIndex];
            var currLink = row.cells[0].innerHTML;
            var currVal = currLink.substring(currLink.indexOf('">')+2, currLink.toUpperCase().indexOf("</A>"));
            //Delete the cell that contains the link
            row.deleteCell(0);
            //Now insert a cell and create the text box that will go in its place for user to edit
            newCell = row.insertCell(0);
            newText = document.createElement("input");
            newText.type = "text";
            newText.size = "60";
            newText.className = "text";
            newText.id = "noteEdit";
            newText.value = currVal;
            newCell.appendChild(newText);
            //Add the spaces between the textbox and button
            newText = document.createTextNode(" ");
            newCell.appendChild(newText);
            newText = document.createTextNode(" ");
            newCell.appendChild(newText);
            //Now create and add the button
            newBtn = document.createElement("input");
            newBtn.type="button";
            newBtn.id = "editNote";
            newBtn.value = "+";
            newBtn.onclick = function() {editNoteDB(rowIndex, noteId);}
            newCell.appendChild(newBtn);
        }
        function editNoteDB(rowID, noteID) {
            //This function will call the ASP page (via
            //ajaxFunction) to edit the note in the database.
            newValue = document.getElementById("noteEdit").value;
            var tblRef = document.getElementById("noteTbl");
            var row = tblRef.rows[rowID];
            row.deleteCell(0);
            newCell = row.insertCell(0);
            newText = document.createTextNode(newValue);
            link = document.createElement("a");
            link.href="#";
            link.onclick = "javascript:editNote("+noteID+",this.parentNode.parentNode.rowIndex);";
            link.appendChild(newText);
            newCell.appendChild(link);
        }
</script>
<body>
    <table id="noteTbl">
        <tr>
            <td><a href="#" onclick="javascript:editNote(9003,this.parentNode.parentNode.rowIndex);return false;">STRAIGHT THROUGH ASSEMBLY.</a></td>
        </tr>
    </table>
</body>

Open in new window

0
Comment
Question by:chrscote
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 

Expert Comment

by:tsibusha
ID: 24827222
The "#" in a URL (used in the href attribute) means a point within the page. For example, page.htm#chapter1, page.htm#chapter2, etc.

"javascript:" is a syntax for URLs, instead of "http:". So to create the link, you don't need "onclick" at all. All you need is:


link.href = "javascript:doSomething()"

Open in new window

0
 

Author Comment

by:chrscote
ID: 24850564
By putting the javascript call in the href, I am getting an error when determining the rowIndex of the table.  I get the error message: 'this.parentNode.parentNode.rowIndex' is null or not an object.  If it is in the onclick event, however, this works.
0
 

Author Comment

by:chrscote
ID: 24858434
OK, I've decided to try something else.  Since I'm using ASP to create the original list, I am going to use a counter to include the "rowIndex" rather than using the .rowIndex property.  This appears to work, though I was hoping for more generic code that could be used when I'm not using ASP.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:tsibusha
ID: 24860099
You mean, using rowIndex to identify the link? A <tr> object always has a rowIndex that you can access and use. You don't need ASP for this.
0
 

Author Comment

by:chrscote
ID: 24865084
What I mean is that, when I try to use
<a href = "javascript:editNote(9003, this.parentNode.parentNode.rowIndex)">,
I get an error saying that parentNode.parentNode doesn't exist.  However, if I just use ASP to determine which row I'm in so that the link appears as::
<a href = "javascript:editNote(9003, 3)">
It works fine.  I've tried using just this.parentNode.rowIndex as well as simply this.rowIndex as well with the same issues.

Chris
0
 

Accepted Solution

by:
chrscote earned 0 total points
ID: 24960553
I've never received an accepted solution, so I will close it now.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RSS Feeds--IE 13 195
Assess if java script has insecure coding issue 1 164
Problem to echo 6 117
embadded search engine in website 4 46
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

738 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