Solved

html javascript change value of cell based on contents of another cell

Posted on 2013-12-13
6
261 Views
Last Modified: 2013-12-13
Hi,

I have an html table which is populated with data from a source that we can't edit.  So, any changes we make need to be when the page loads using javascript.

Let's say I have a table which contains the following rows

<table id=animals>
 <tr>
  <td class=td1 id=status>Available</td>
  <td class=td1 id=sub>Horse</td>
 </tr>
 <tr>
  <td class=td1 id=status>Available</td>
  <td class=td1 id=sub>Donkey</td>
 </tr>
</table>

 Where the content of the table cell with id=sub is "Donkey", I want the status field to say "unavailable" instead of "available".  If the content of the field is anything other than "Donkey" the status field should remain unchanged.
 
 How can this be achieved with javascript given that it has to work on a row by row basis?
 
 TIA
0
Comment
Question by:yelbow
  • 3
  • 2
6 Comments
 
LVL 7

Expert Comment

by:dimmergeek
Comment Utility
First of all, you cannot have two objects on the same page with the same id.
Secondly, how are you populating your data table?  What data source are you reading from? Why not just set the correct value when you populate the table?
0
 

Author Comment

by:yelbow
Comment Utility
It comes from a 3rd party app, the only part of it which we can edit is the resulting html.  So, we inherit it like this and can only amend the data at that point.
0
 
LVL 7

Expert Comment

by:dimmergeek
Comment Utility
Does the third party app create the table for you including the duplicate id?  If so, I would go back to them and let them know they have a basic HTML issue.  With that data structure, how will you tell one id=sub from another to make the change?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
Test page : http://jsfiddle.net/PAN56/

function changeStatusFor(content_to_search, newStatus) {
    var animals_table = document.getElementById("animals");
    var cells = animals_table.getElementsByTagName("td");
    var number_of_cells = cells.length;
    for(var i=0;i<number_of_cells;i++) {
        var cell = cells[i];
        var cell_content = cell.innerHTML;
        if(cell_content == content_to_search) {
            var row = cell.parentNode;
            var cell_status = row.getElementsByTagName("td")[0];
            cell_status.innerHTML = newStatus; 
        }
    }
}

window.onload = function() {
    changeStatusFor("Donkey","unavailable");
    changeStatusFor("Horse","unbelievailable");
}

Open in new window

0
 

Author Comment

by:yelbow
Comment Utility
They do, but we can amend that too (though I will mention it to them).

If they had unique IDs would it be possible?

What if the fields had no ID at all?
0
 

Author Closing Comment

by:yelbow
Comment Utility
Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now