Jump to content

Piculiar behavior of Innerhtml


abdfahim

Recommended Posts

Hi,

 

I have 13 buttons and I have used innerhtml to write different thing inside a div depending on which button I click.

<SCRIPT LANGUAGE="JavaScript">
      <!-- Begin
      function secAdetails(q){
            document.getElementById('secA').innerHTML = ''; //SecA is the DIV ID
            alert(myArray[q]);
            document.getElementById('secA').innerHTML = myArray[q];
      }
      // End -->
</script>

 

The javascript array myArray[q] has all the strings stored (so, array lenghth is 13). Now its behaving piculiarly. For example, if I click on Button 7 (which has the largest string to display), it is showing partial text. Also, if I 1st select Button 1 and then Button 7, it always shows first 4 lines, if I choose Button 2  and then Button 7, it always shows first 7 lines etc. Anybody has any idea why this is happening? Btw, I put an alert and in the alert dialog box, it shows wwhole string, just it can't write that completely using innerhtml!

Link to comment
Share on other sites

this works fine for me:

<SCRIPT LANGUAGE="JavaScript">
      <!-- Begin
      myArray = [
        '<b><u>Roberto</u> : </b><br>questions should be more practical.<br><br><b><u>This is another name</u> : </b><br>Here is another text.<br><br><b><u>',
        'Here is another string'
      ];
      function secAdetails(q){
            document.getElementById('secA').innerHTML = ''; //SecA is the DIV ID
            alert(myArray[q]);
            document.getElementById('secA').innerHTML = myArray[q];
      }
      // End -->
</script>
<input type="button" value="Button 1" onclick="secAdetails(0);" />
<input type="button" value="Button 2" onclick="secAdetails(1);" />
<div id="secA"></div>

 

can you post a snippet of code that doesn't work for you...that way i can see what you see

Link to comment
Share on other sites

Hi,

 

I was wondering if I have some problem in my coding, why would it act pseudo-random way. Like, the amount of text its displaying depends on what I click before clicking this button. If I click button A and then the concern button, it always shows certain amount of text, if I click button B and then the concern button, it always shows another amount of text. But this amount is constant. I mean if my sequence is ButtonA, then concern button, it always shows same amount of text, though its partial.

 

Anyway, here is my code.

 

<?php
      include("config.php");
      echo "<HTML><HEAD> <TITLE></TITLE>";
?>
<SCRIPT LANGUAGE="JavaScript">
      var myArray = new Array();
</SCRIPT>
<?php
            echo "</HEAD><BODY>";
            
            for($m=0;$m<13;$m++){
                  echo "<a style=\"cursor: hand;\" onclick=\"javascript: secAdetails(".$m.");\">Q".($m+1)."</a><BR>";
            }
            
            echo "<div id=\"secA\" style=\"text-align:left; overflow: auto; width: 98%;\">";
            echo "</div>";
            for($x=0;$x<13;$x++){
                  $myquery="Select username, q".$x."_comm as com from plan_survey";
                  $result=mysql_query($myquery) or die(mysql_error());
                  if(mysql_num_rows($result)>0){
                        while($row=mysql_fetch_assoc($result)){
                              if(!get_magic_quotes_gpc()) {
                        	      $name = addslashes($row['name']);
                        	      $com = addslashes($row['com']);
                              }else{
                                    $name = $row['name'];
                                    $com = $row['com'];
                              }
                              $com=str_replace("\r\n","",$com);
                              $com=str_replace("\n","",$com);
                              $temp = "<b><u>".$name."</u> : </b><br>".$com."<br><br>";
                              //echo $temp;
                              echo "<SCRIPT LANGUAGE=\"JavaScript\">myArray[".$x."]='".$temp."';</SCRIPT>";
                        }
                  }
                  mysql_free_result($res1);
            }
?>
<SCRIPT LANGUAGE="JavaScript">
      <!-- Begin
      function secAdetails(q){
            alert(q);
            document.getElementById('secA').innerHTML += myArray[q];
      }
      // End -->
</script>      
<?php
            echo "</BODY></HTML>";
?>

Link to comment
Share on other sites

i looked it over again, and there was some stuff i found...try this out:

<?php
  include("config.php");
  $links = array();
  $data = array();
  for($x=0;$x < 13;$x++){
    $query = "SELECT username, q".$x."_comm AS COM FROM plan_survey";
    $result = mysql_query($query) or die(mysql_error());
    if(mysql_num_rows($result) > 0){
      while($row=mysql_fetch_assoc($result)){
        $data[] = sprintf('<b><u>%s</u> : </b><br>%s<br><br>',htmlspecialchars($row['name']),htmlspecialchars($row['com']));
      }
    }
    $links[] = sprintf('<a style="cursor:hand;" onclick="secAdetails(%s);">Q%s</a><br />',$x,$x+1);
    mysql_free_result($result);
  }
?>
<html>
  <head>
    <title></title>
    <script type="text/javascript">
      <!-- Begin
      var myArray = <?php echo json_encode($data); ?>;
      function secAdetails(q){
            alert(q);
            document.getElementById('secA').innerHTML += myArray[q];
      }
      // End -->
    </script>
  </head>
  <body>
<?php echo implode("\n",$links); ?>
    <div id="secA" style="text-align:left;overflow:auto;width:98%;"></div>
  </body>
</html>

Link to comment
Share on other sites

hey rhodesa, sorry i was out of the town so i m late in reply. anyway, it'll be better if you say exactly where in the code you are suspecting the problem. then i'll change there. I can't exactly copy ur code because the code i post is just a portion of my full code. coz, otherwise it would be so large that you might fill disgusted. And I will paste the table with some data at the earliest possible time. Thanks for ur help.

Link to comment
Share on other sites

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.