Jump to content

Simple question, how to make javascript work with mutliple rows?


Recommended Posts

I know that there are tutorials, but I would like to understand what small changes are needed to change a javascript function from working with one row to working with multiple rows.  For example, if you use:

 

<script type='text/javascript">

function displayDate()

{

document.getElementById("demo").innerHTML=Date();

}

</script>

 

<span id="demo" onclick="displayDate()">description</span>

 

That works, but only with one row on the page.  How can I change that to work with multiple rows? 

The main problem is determining which elements you want to apply the event to.  There are a variety of ways to do it, but it depends on what you actually want to do with the actual elements in your document.  There's no single answer for all cases.

 

So, if you have a particular problem, show more code.

Thanks, I am slowly trying to learn javascript so I am doing some test code and here is what I have so far to make an area of text get inserted into a text area and later I could use jquery to update it in the database.  Here is what I have so far:

 

function displayDate(str)
{
document.getElementById("demo").innerHTML="<textarea name=\"editdesc\" cols=\"120\">" + str + "</textarea> <input type=\"button\" onclick=\"UpdateRow()\" value=\"Update\">";
}

function UpdateRow()
{
document.write("hello");
}

 

then the html that uses multiple entries:

 

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><b>Description</b>: <span id="demo" onclick="displayDate('Admin, hello! here are having problems with your site. Write me. ECK 98956785664')">Admin, hello! here are having problems with your site. Write me. ECK 98956785664</span></td>
  </tr>
  <tr> <b>Description</b>: <span id="demo" onclick="displayDate('A question for both genders. Guys, do you feel the need to go all out and spend all your money on an engagement ring to make your girl happy?      
      
Ladies, does the cost of the ring actually increase how much love you feel for your man?       
       
I dont have a large amount of money but the ring I bought for my woman the other day s')">A question for both genders. Guys, do you feel the need to go all out and spend all your money on an engagement ring to make your girl happy?      
    
    Ladies, does the cost of the ring actually increase how much love you feel for your man?       
    
    I dont have a large amount of money but the ring I bought for my woman the other day s</span>
      </td>
  </tr>
</table>

 

 

Well, right now you have two spans with the ID of 'demo', so you'll first need to change that.  Elements can't share the same ID.

 

And, like I said before, it depends on what you want to do, exactly.  You could simply do something like (pseudo-code):

 

var rows = document.getElementsByTagName('tr'); // change depending on what kind of element you need
var matches = new Array();

for(var i = 0; i < rows.length; ++i) {
   if (/* rows[i] matches a regex pattern for your collection of IDs, like demo1, demo2, demo3, etc. */) {
      matches.push(rows[i]);
   }
}

for(var j = 0; j < matches.length; ++j) {
   matches[j].onclick = function() {
      // event handling code
   }
}

Thanks, yes I am basically asking how to change a function that works with one case to make it work with multiple rows.  I want to be able to understand what changes are made so I can edit existing functions that are standalone.

And, like I said before, it depends on what you want to do, exactly.

 

yes I am basically asking how to change a function that works with one case to make it work with multiple rows.

 

Ha.. Perhaps ask something "exact"? I would recommend first reading up on how to work with arrays and objects - those are the key to writing these type of scalable functions. It's quite a large subject though for Nightslyr, or anyone to explain it all to you in 'general'. It's the kind of thing you get better at the more experienced you become.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.