Jump to content

retrieve value from repeat region using onclick


xjx424

Recommended Posts

  • Replies 52
  • Created
  • Last Reply
<script src="../../SpryAssets/SpryMenuBar2.js" type="text/javascript"></script><link rel="shortcut icon" href="../../images/favicon.ico" type="image/x-icon"><link href="../../styles/main_styles.css" rel="stylesheet" type="text/css" /><script type="text/javascript">function init () { var links = document.getElementsByTagName("a"); var bodies = document.getElementsByTagName("input"); var bod = new Array(); for (var i2 = 0; bodies[i2]; i2++) {  bod[i2] = bodies[i2].value; } for (var i = 0; links[i]; i++) {  links[i].onclick = function hiya () {   document.getElementById("body_text").value = bod[i];  } }}window.onload = init;</script>

Figure out how to make bod a global variable in that script and you got your answer.

Link to comment
Share on other sites

Wait. The bod is going to be bod[21] because i is 21 after the loop has played itself out. 21 is undefined because that is the button, (i didn't know how else to call all your ids then to add the button at the end). Figure out how to make i stored as a variable each time or something then make bod[thatvariable] at the end.

Link to comment
Share on other sites

I GOT IT!!!!!!! lol :) OK but really in my excitement I made a mistake so I'm editting this.EDIT: I'm taking out the alert and removing the comment where it spits out the function so it can go in the body. and also you can take out bodd variable at the top. I was experimenting with that but it didn't work out

<script type="text/javascript">bod = new Array();function init () { var links = document.getElementsByTagName("a"); var bodies = document.getElementsByTagName("input"); for (i2 = 0; bodies[i2]; i2++) {  bod[i2] = bodies[i2].value; } for (i = 0; links[i]; i++) {  links[i].onclick = function hiya () {   document.getElementById("body_text").value = bod[this.id.match(/[0-9]+$/)];  }  links[i].id += i }}window.onload = init;</script>

Link to comment
Share on other sites

Hey man I was learning myself that whole time. I had to stretch my knowledge of javascript as far as it goes for that. lol I enjoyed working on it (to a point). You are welcome and good luck with your future scripts.Eru

Link to comment
Share on other sites

Hi Guys,FYI document.getElementById, there's no "s" in it, why? Because ID name is supposed to be unique, just one, why on document.getElementsByTagName, because you can return a tag name of the selected element in a document in many instances.

Link to comment
Share on other sites

Thanks Mark. I spent 4 and a half hours figuring THAT one out...*sigh* I wish you were here earlier. haha

Link to comment
Share on other sites

Eru,I'm hoping you can help me out with one more thing. I noticed that when I added text to the values being passed to the new text area it would make the array off by 1. When I just use numbers it works fine. I'm guessing it might have something to do with the regular expression you used here "bod[this.id.match(/[0-9]+$/)];". Does that sound possible?Thanks

Link to comment
Share on other sites

That code there bod[this.id.match(/[0-9]+$/)] is simply taking the number value in the id of the (hidden) input box, which I gave to them using links[i2].id += i2 or something like that. Unless you changed the number value off of the end of the id, that shouldn't effect it.So you are saying that John's message is "1"and the next person is "2". That works fine?But then you say that when you say John's is "Hi Im john"and the next person is "hi im kate" then it is not working?

Link to comment
Share on other sites

Well, I took all of the inputs and gave their values to the bod[number of input button]. I took a look at your search bar. You have added some inputs, so I think that may have thrown it off. Click date sent link above the emails, and your first e-mail comes up for a second, until the page refreshes. We will have to come up with another way to do this.EDIT: you added links and inputs. When i made your script, I didn't account for the fact their would be other links and inputs. This could take a while again. lol

Link to comment
Share on other sites

and when you click "subject" it reads "search" in the bodyI played around with changing this:

var links = document.getElementsByTagName("a");

to

var contain = document.getElementById("mail_list")var links = contain.getElementsByTagName("a");

with no success

Link to comment
Share on other sites

Actually. It ended up to be quite a simple solution. Here is a functional code:

function init () {var links = document.getElementsByTagName("a");var bodies = document.getElementsByTagName("input");for (i2 = 0; bodies[i2]; i2++) {bod[i2] = bodies[i2].value;}for (i = 0; links[i]; i++) {links[i].onclick = function hiya () {document.getElementById("body_text").value = bod[this.id.match(/[0-9]+$/)];}[b]links[i].id += i-1[/b] // HERE IS WHERE I CHANGED}}window.onload = init;

The only problem present in this is as you build up that page more you will probably make more links and need to change it more. That means this probably isn't the best way to do what you need to do.

Link to comment
Share on other sites

No problem. If you end up putting a lot more links or inputs into that thing, I can remake your script. i'm sure it won't be too hard to base it off of what you have now.

Link to comment
Share on other sites

Here ya go bud:

bod = new Array();function init () {var links = document.getElementById("mail_list").getElementsByTagName("a");var bodies = document.getElementById("mail_list").getElementsByTagName("input");for (i2 = 0; bodies[i2]; i2++) {bod[i2] = bodies[i2].value;}for (i = 0; links[i]; i++) {links[i].onclick = function hiya () {document.getElementById("body_text").value = bod[this.id.match(/[0-9]+$/)];}links[i].id += i}}window.onload = init;

That will only get links and inputs inside the div id'd as mail_list

Link to comment
Share on other sites

I really thought I had this one figured out but came across another input issue. I have added checkboxes inside the "email_list" div. I attempted using getAttribute to filter each before assigning a value to "bod[i2]" but that didn't do it. Do you think I'm on the right track?I've now added

if (bodies[i2].getAttribute("type") == 'hidden') {

in this

bod = new Array();function init () {var links = document.getElementById("mail_list").getElementsByTagName("a");var bodies = document.getElementById("mail_list").getElementsByTagName("input");for (i2 = 0; bodies[i2]; i2++) {if (bodies[i2].getAttribute("type") == 'hidden') {bod[i2] = bodies[i2].value;}}for (i = 0; links[i]; i++) {links[i].onclick = function hiya () {document.getElementById("body_text").value = bod[this.id.match(/[0-9]+$/)];}links[i].id += i}}window.onload = init;

Link to comment
Share on other sites

No, that doesn't seem right. Let me ponder this some more. the get attribute might be a way to do it but I got to think and I'm just about ready for bed.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...