Solved

I want to write this function once. How?

Posted on 2014-02-26
2
152 Views
Last Modified: 2014-03-01
Here's my challenge: I have a list could have one row, it could have several. It's coming in as an array so it looks like this:

  <tr>
                           <td width='".($leftcol-25-100)."'><span class='info'>".$allocatedlist[$i]["shortname"]."</span></td>
                    <td width='75'><span class='info'>$".number_format($allocatedlist[$i]["totalcost"],2)."</span></td>
                    <td width='75'><span class='info'><input type='text' size='10' class='cer' name='allocated' value='$".number_format($allocatedlist[$i]["allocatedamount"],2)."' onchange='visibility()'></span></td>
                    <td>&nbsp;<div id='save_".$i."' style='visibility:hidden; display:inline; vertical-align:center;'><img src='images/save_icon.gif' width='15></div> 

Open in new window


The bottom line is that on every row, I have an input field (allocated). Should my user make any changes to that dollar amount, as soon as they finish, my save icon become visible to the immediate right.

My JavaScript looks like this:

function visibility() {
    document.getElementById('save_1').style.visibility = 'visible';
}

Open in new window


You'll notice that my ID is "save_1." I started playing with the id on the HTML side and you can see the beginning of that thought process where you see ;<div id='save_".$i."'

I'm thinking that this is possible: I want to pass that "$i" value into my JavaScript function so that it makes the correct row visible and I'm not having to write the same script over and over again, which I couldn't in any case because I'm never going to have the same number of rows.

So, how can I pass that "$i" value into my script so I can have that save icon show up on every row?

Looking forward to getting some input!
0
Comment
Question by:brucegust
[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
2 Comments
 
LVL 19

Accepted Solution

by:
Ken Butters earned 250 total points
ID: 39889933
Thinking you should be able to use "this".
<input type='text' size='10' class='cer' name='allocated' value='$".number_format($allocatedlist[$i]["allocatedamount"],2)."' onchange='visibility(this)'>

Open in new window

Then in your javascript routine something like this :

I didn't test... so may not have the relationship between the nodes accurately reflected... but I think you probably get the idea.

function visibility(obj) {
   var objectToChange;
    objectToChange = obj.nextSibling;
    objectToChange = 'visible';
}

Open in new window

0
 
LVL 9

Assisted Solution

by:Sar1973
Sar1973 earned 250 total points
ID: 39891329
Try to replace onchange='visibility()' with onchange='visibility(this.name);' and use this function:
function visibility(myObject) {
    document.getElementById("myObject").style.display="inline";
}
Otherwise, set:  
onchange="document.getElementById('this.name').style.display='inline';" directly.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I add counter to html listbox using jquery 2 42
What is the correct way to check for empty value in input field 2 34
Make Float not to Wrap 15 65
JSON help 8 44
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

737 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