FRIHOSTFORUMSSEARCHFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Why this code dont work ?

 


Philip
why this code don't work
Code:

<form name="frmpostkuis">
<input type="radio" name="r[1]" value="a" onClick="alert(document.frmpostkuis.r[1].value)">
</form>


but why this code work ?
Code:

<form name="frmpostkuis">
<input type="radio" name="r[1]" value="a" onClick="alert(this.value)">
</form>


what should i do ? please someone suggest me thanks
aningbo
confues me... can we have the form?
Traveller
It may depend upon the browser you use. In particular, I have noticed that IE wants parentheses ("(" and ")") for some subscripts, while almost any other browser (esp. Firefox and Opera) want brackets ("[" and "]"). Thus, your
Code:
document.frmpostkuis.r[1].value
may not work properly in IE, but may need to be
Code:
document.frmpostkuis.r(1).value


To make it universal, you may need to do something like
Code:
var browsertype=navigator.appName;
if (browsertype=="Microsoft Internet Explorer") {
...document.frmpostkuis.r(1).value...
} else {
...document.frmpostkuis.r[1].value...
}


Good luck!
Aredon
The fix is simple.

Failing to put r[1] it in quotes will cause JavaScript to think the element's name is "r" and the [1] part will cause an error because it is not an array or nodelist.

Simply use bracket notation so you can put the name in quotes:
Code:

onclick="alert(document.frmpostkuis['r[1]'].value)"
hellrahul
Hey ! I can't understand if second one is working why are you behind fst one. Twisted Evil
Aredon
The original poster most-probably is referencing the element from another element or encountered the issue and just wants to learn how to reference the element or both.

As far as Traveller's solution, I HIGHLY doubt he tested the original poster's code nor took the time to test his own code with it because it fails to reference the element -- he probably just assumed the element was a nodelist and tried to reference it as such. The element is NOT an array or nodelist hence why his approach fails in this situation. And in such a case that the element WOULD be a nodelist, his approach would work however require browser decection which is an obscure approach for referencing nodelists. IE supports both parenthesis notation as well as bracket notation and bracket notation is cross-browser while parenthesis notation is IE-only. In otherwords, when working with nodelists, use bracket notation and you won't need to resort to browser detection.

Back to the original poster's situation, I present the solution once again:
Code:

<form name="frmpostkuis">
<input type="radio" name="r[1]" value="a" onClick="alert(document.frmpostkuis['r[1]'].value)">
</form>
Philip
hellrahul wrote:
Hey ! I can't understand if second one is working why are you behind fst one. Twisted Evil


i need to call script that using this object, so i cannot write this.value, because it will belong to others object.
anyway i am using getelementbyid for solved this problem Smile

since it didn;t check array or what ever thing on id.
izcool
Solution that I've come up with.

Don't put the [ and ] around the 1, as in IE, the JavaScript error read this :

Code:
Line: 2
Char: 1
Error: 'document.frmpostkuis.r.1' is a null or not an object
Code: 0
URL: file://C:\Documents and Settings\Mike\Desktop\test.html


It looks like it's reading that as "document.frmpostkuis.r.1" instead of "document.frmpostkuis.r[1]".

Code that works :

Code:
<form name="frmpostkuis">
<input type="radio" name="r1" value="a" onClick="javascript:alert(document.frmpostkuis.r1.value);">
</form>


- Mike.
JayBee
There is not solution but I thing it helps izcool to answer his question why to use it.

izcool wrote:
Solution that I've come up with.

Don't put the [ and ] around the 1, as in IE, the JavaScript error...

But it is not perfect solution Wink because if you post some variable called r[1] to php script, it will create array r with key 1.
If you have form like
Code:
<form>
  <input  name="r[1]" value="a">
  <input  name="r[2]" value="b">
  <input  name="r[3]" value="c">
</form>

you will get this aray
Code:
$r = Array
(
    [1] => a
    [2] => b
    [3] => c
)

but this form
Code:
<form>
  <input  name="r1" value="a">
  <input  name="r2" value="b">
  <input  name="r3" value="c">
</form>

give you 3 variables
Code:
    $r1 = a
    $r2 = a
    $r3 = c

and you must do bad things to process these variables like
Code:
for($i=1; $i<4; $i++) {
   echo ${"r$i"};
}

insted of using foreach or any other PHP stuff.
Code:
foreach($r as $item) {
   echo $item;
}

isn't it easier? Razz
Philip
there is another strange, there;s many said it is'nt array when we using

<input type="radio" name="r[1]" value="a">

but after that after u post using a form
<form method="Post" action="blablabla.php">
<input type="radio" name="r[1]" value="a">
</form>

on that php u can get the r[1] value it using like this $_POST["r"][1]
, so is it array or not ? -0- ?
Aredon
PHP threats fields with brackets in their name as arrays. JavaScript doesn't.
Code:

<form method="Post" action="blablabla.php">
<input type="radio" name="r[1]" value="a" onclick="alert(this.form.elements['r[1]'].value)" >
</form>
Philip
i try to switch position, but why this code don;t work ??

Code:
<script language="javascript">
   function Down(posisi)
   {
      a=document.frmpartner['txtsite['+(posisi+1)+']'].value;
      b=document.frmpartner['txtket['+(posisi+1)+']'].value;
      c=document.frmpartner['txtlink['+(posisi+1)+']'].value;
      d=document.frmpartner['chkdel['+(posisi+1)+']'].checked;
      document.frmpartner['txtsite['+(posisi+1)+']'].value=document.frmpartner['txtsite['+(posisi)+']'].value;
      document.frmpartner['txtket['+(posisi+1)+']'].value=document.frmpartner['txtket['+(posisi)+']'].value;
      document.frmpartner['txtlink['+(posisi+1)+']'].value=document.frmpartner['txtlink['+(posisi)+']'].value;
      document.frmpartner['chkdel['+(posisi+1)+']'].checked=document.frmpartner['chkdel['+(posisi)+']'].checked;
      document.frmpartner['txtsite['+(posisi)+']'].value=a;
      document.frmpartner['txtket['+(posisi)+']'].value=b;
      document.frmpartner['txtlink['+(posisi)+']'].value=c;
      document.frmpartner['chkdel['+(posisi)+']'].checked=d;
   }
</script>


i using like this on named my object txtlink[],txtket[]....
and call the function like this Down(0) <-- switch one with tw0 position.

thanks,
Aredon
Give this a try:
Code:

<script type="text/javascript">
   function Down(posisi)
   {
      var f=document.frmpartner;
      var els=new Array( 'txtsite[]', 'txtket[]', 'txtlink[]', 'chkdel[]' );
      var a=f[ els[0] ][posisi+1].value
      var b=f[ els[1] ][posisi+1].value
      var c=f[ els[2] ][posisi+1].value
      var d=f[ els[3] ][posisi+1].checked;
      f[ els[0] ][posisi+1].value=f[ els[0] ][posisi].value;
      f[ els[1] ][posisi+1].value=f[ els[1] ][posisi].value;
      f[ els[2] ][posisi+1].value=f[ els[2] ][posisi].value;
      f[ els[3] ][posisi+1].checked=f[ els[3] ][posisi].checked;
      f[ els[0] ][posisi].value=a;
      f[ els[1] ][posisi].value=b;
      f[ els[2] ][posisi].value=c;
      f[ els[3] ][posisi].checked=d;
   }
</script>
Philip
thanks, it;s working ^_^
Related topics
who is mohammad? the prophete of islam
INCLUDE(FILENAME) -------
100 days free licence for Kaspersky Internet Security 2009
phpBB2 ERROR PLEASE HELP
[c++]random number generator doesnt work
links dont work in IE from frihost hosted site
Why this PNG code don;t work ?
Why does this code make undeletable files?
users online, using php and text file
Movement rotation action scrip
Center IFrame
Flash - Why won't this work?
2 things that dont work
need help with this code
Thumbnail Generation !
Server Side Include (HTML in HTML)
e-mail error *sigh*
php dynamic graph code not working
Free Coding in XHTML, CSS and a little bit of JavaScript.
mail()
[OFFICIAL] PlayStation 3
Politicaly incorrect blonde jokes
DOUBLE HDD AND FTP
HOWTO?$lang files!
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.