neha
neha

Reputation: 11

javascript not working with hiddenfield

i want to find hiddenfield in my javascript but it is showing undefined value.

myscript is,

<script type="text/javascript">      
             var i = document.getElementById('HiddenField4').value;
              while (i < cn) {
              photoslink[i] = cnSplit[i];
              photos[i] = b[i];
              i++;
          }            
 alert(i);

         var mygallery2 = new fadeSlideShow({

             wrapperid: "fadeshow2", //ID of blank DIV on page to house Slideshow
             dimensions: [568, 313], //width/height of gallery in pixels. Should reflect dimensions of largest image
             imagearray: [
  ["images/1.jpg", "", "", ""],
  ["images/2.jpg", "", "", ""],
  ["images/3.jpg"],
  ["images/4.jpg", "", "", ""] //<--no trailing comma after very last image element!
 ],
             displaymode: { type: 'auto', pause: 2500, cycles: 0, wraparound: false },
             persist: false, //remember last viewed slide and recall within same session?
             fadeduration: 500, //transition duration (milliseconds)
             descreveal: "always",
             togglerid: "fadeshow2toggler"
         })
</script>

Upvotes: 0

Views: 866

Answers (5)

Azhar
Azhar

Reputation: 20670

Actually server controls id is different from its client id

so you can do it like that

<script type="text/javascript">    

var HiddenField4 = '<%=HiddenField4.ClientID %>';

var i = document.getElementById('HiddenField4').value;
 while (i < cn) {
              photoslink[i] = cnSplit[i];
              photos[i] = b[i];
              i++;
          }            
 alert(i);

         /* remaining code here*/

</script>

Upvotes: 0

VinayC
VinayC

Reputation: 49175

I can think of two possibilities - first that HiddenField4 being a server control, so in that case, you need to use its client id. You can use syntax such as

var i= document.getElementById('<%=HiddenField4.ClientID %>').value;

Second issue can be that this script is placed at top of page while your hidden element is below. So when script gets executed, hidden field does not exist. Solution is either to move the script block at the bottom of page or to use some event (load on body) to execute script after page is loaded. For example,

<script type="text/javascript">  

   function executeMyScript() { 
             var i = document.getElementById('HiddenField4').value;
              while (i < cn) {
              photoslink[i] = cnSplit[i];
              photos[i] = b[i];
              i++;
          }            

         var mygallery2 = new fadeSlideShow({
       ... // rest of the script
 }
</script>

And use onload event on body element

... // header and script
<body onload="executeMyScript();">
... // rest of the html

Upvotes: 2

fcalderan
fcalderan

Reputation:

Is your script located or executed after the element whose id is HiddenField4 ? Why don't you run that snippet on domready/onload?

Upvotes: 1

Joachim VR
Joachim VR

Reputation: 2340

I doubt 'HiddenField4' is the actual client-side ID of the hidden field control. You should replace the 'HiddenField4' by '<%= HiddenField4.ClientID %>'. ASP.NET changes the id of any control to a more unique, hierarchic form, which can be found in the .ClientID property.

Upvotes: 1

Buhake Sindi
Buhake Sindi

Reputation: 89169

Does element HiddenField4 exist? If so, have you checked that it has the same case?

Upvotes: 1

Related Questions