setting variable value through onkeyup

M

Mariano

I have this code, when it's called it print the HTML form written in
the form variables in a div called divForm, a simple Ajax.

function myFunc(){
var form, ris;

form = "<form action=\"#\" >Some text<br><br>";
form += "<input type=\"text\" name=\"valore\" value=\"aValue\"
onkeyup=\"ris = this.value\" />";
form += "<input type=\"submit\" name=\"act\" value=\"Send\" >";
form += ris;
form += "</form>";

var target = document.getElementById("divForm");
target.innerHTML=form;
}

Now I would that, every letter written in the input box, will be
replayed in the ris variable too and showed in the current page, i've
tried with onkeyup=" ris = this.value" but 'undefine' is always
returned to me.

Any ideas???
ty
 
T

tomtom.wozniak

I have this code, when it's called it print the HTML form written in
the form variables in a div called divForm, a simple Ajax.

function myFunc(){
    var form, ris;

    form = "<form action=\"#\" >Some text<br><br>";
    form += "<input type=\"text\" name=\"valore\" value=\"aValue\"
onkeyup=\"ris = this.value\" />";
    form += "<input type=\"submit\" name=\"act\" value=\"Send\" >";
    form += ris;
    form += "</form>";

    var target = document.getElementById("divForm");
    target.innerHTML=form;

}

Now I would that, every letter written in the input box, will be
replayed in the ris variable too and showed in the current page, i've
tried with onkeyup=" ris = this.value" but 'undefine' is always
returned to me.

Any ideas???
ty

I think you're overwriting and resetting your form everytime the
onkeyup event fires. Even though the values of an element might
change via a javascript call, the elements themselves within your
document need to be non-volatile (e.g. span id="ris").

Let me know if the snippet below helps your cause. :)

<!-- Tested in IE7 and Firefox 2 -->

<script>
function fromValueShowLastChar (text, obj) {
obj.innerHTML = text.substring(text.length-1);
}
</script>

<div align="center">
<span id="ris" style="font:bold small-caps 900 48px arial">A</span>
<br>
<input type="text" name="myInput" value="A"
onkeyup="fromValueShowLastChar(this.value,ris)" />
</div>
 
M

Mariano

(e-mail address removed) said the following on 1/24/2008 2:04 PM:







You may want to re-test it in Firefox 2, ris is not defined.


From the original code, they don't want the last character, they want
the entire value


As it is, it only gives the last character typed.

obj.innerHTML = text.value;

'text' isn't a good variable name.



onkeyup="fromValueShowLastChar(this.value,'ris')"

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/


and if I need to bind this value to a GET string of a link, what can i
do?
 
T

tomtom.wozniak

and if I need to bind this value to a GET string of a link, what can i
do?- Hide quoted text -

- Show quoted text -

Sorry, Randy. The code definitely works in FireFox - Mozilla/5.0
(Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/
2.0.0.7. It wouldn't be "text.value" because text IS a good variable
name. It's the "this.value" from the call:
fromValueShowLastChar(this.value,ris). The ris id is valid is this
context as well.

I posted an image at http://www.postyourimage.com/view_image.php?img_id=RZqAQVxNxJtMIxG1201209264
to prove my code works in FF. Please, if you did get some kind of an
error, I'd like to see it.

I did, however, change the text so it wasn't just spitting out the
last character. The changed code will provide an href that is updated
with whatever text is provided.

<script>
function fromValueShowLastChar (text, obj) {
obj.innerHTML = "<a href='http://rot13.com/index.php?
text=INPUT'>Translation at rot13.com!</a>".replace(/INPUT/
g,text.replace(/\ /g,"+"));
}
</script>

<div align="center">
<span id="ris" style="font:bold small-caps 900 48px arial">URL</
span>
<br>
<input type="text" name="myInput"
onkeyup="fromValueShowLastChar(this.value,ris)" />
</div>
 
M

Mariano

Mariano said the following on 1/24/2008 3:13 PM:




Testing in Firefox, with 'ris' corrected, will show what else is wrong
with the line above. The answer to why FF won't like it is in the group FAQ.







Huh? Bind it to a GET string of a link? Might be better if you explain a
little better what you are trying to do. As that question doesn't make a
lot of sense.

In this form, i have a link <a href="miolink.php?id=value">LINK</a>
the id value have to be the same value written in input box.
 
T

tomtom.wozniak

Mariano said the following on 1/24/2008 4:26 PM:



See tomtom's reply, and mine to him, and it will do what you want. Just
make sure you change the IE-ism out of it.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/

Randy, thanks for the reply. I do not have the IE plug-in (such
heresy with not be tolerated!) but did get the same error as you've
shown when using FF at home - Mozilla/5.0 (Windows; U; Windows NT 5.1;
en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11. I tested again
at work, upgrading from 2.0.0.7 to 2.0.0.11 at didn't get any errors
(WTF?) although I got warnings a-plenty about using global references
and one about the font attribute where I had specified "bold". I even
changed the "text" reference to "txt" - if this were meant to be
production code, I can assure you that I wouldn't have chosen that
name. Anyway, I made the changes which you've stated to make the
javascript standards compliant.

I think as time moves forward, we'll see Microsoft driving IE further
and further away from the standards we all should be using (or should
we all be using Microsoft's "standards"?). I'm glad you replied and
showed me the errors (duh, I should've been looking in the error
console instead of just seeing how the browser was responding to that
darn script) although I thought you were pulling my leg at first.

The script below returns no errors nor warnings within FF 2.0.0.11.
Cheers!

<script>
function fromValueShowLastChar (txt, obj) {
obj.innerHTML = "<a href='http://rot13.com/index.php?
text=INPUT'>Translation at rot13.com!</a>".replace(/INPUT/
g,txt.replace(/\ /g,"+"));
}
</script>

<div align="center">
<span id="ris" style="font: small-caps 900 48px arial;">URL</span>
<br>
<input type="text" name="myInput"
onkeyup="fromValueShowLastChar(this.value,document.getElementById('ris'))"/</div>

-Tom Woz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top