SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Call a function twice.

    hey all...

    I have this javascript function: which limits the num of characters displayed on each line:

    function formatStr(EL,maxchars)
    // the EL is the target element passed fron the invokin process in the form:
    // maxchars is no of characters per line or the nth character at which text to be wrapped. For Ex At 15th character//
    // document.getElementById('idofelement');
    {
    strbuff=EL.innerHTML;
    // takes the content of the eleemtn and puts it into a string
    newstr='';
    startI = 0;
    max=maxchars;
    str='';
    subarr=new Array(parseInt(strbuff.length/max+1));
    // creates an array with a length that will hold all max character segments + the final bit.
    // in the earlier versio I was using 7 which is incorrect 15 is the required size
    for (i=0;i<subarr.length;i++)
    {
    // this loop creates max character substrings an put them in anarray
    subarr[i]=strbuff.substr(startI,max);
    startI+=max;
    }
    for (i=0;i<subarr.length-1;i++)
    {
    // this loop creates a new string by concatenating the elements in the array
    // with an HTML line break tag between each segment
    newstr+=subarr[i]+'<br />';
    }
    str+=subarr[subarr.length-1];
    // the final segement is append outside to loop to avoid an extra linefeed
    EL.innerHTML=newstr;
    // the content of the target element is replaced with the new string
    }

    </script>


    i call this function in body like this

    <body MS_POSITIONING="FlowLayout" onload="formatStr(document.getElementById('tdRef1'),40)">


    an then i reference it to the label like this

    <td id="tdRef1">
    <asp:Label ID="lblDisplaySecCusName" Runat="server" CssClass="label"></asp:Label></td>

    its all works but fine but when i want to use this function to another label it doesnt work :S it seems i can only use it for one label but i want to be able to use it for two...


    any ideas ppl???

    thanks

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it works once it should be fine multiple times.

    I suggest you get rid of the body onload attribute and create a function in the <head> section like this:
    Code:
    window.onload = function() {
    formatStr(document.getElementById('tdRef1'),40);
    formatStr(document.getElementById('tdRef2'),40);
    formatStr(document.getElementById('tdRef3'),40);
    // etc
    }

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jimfraser
    If it works once it should be fine multiple times.

    I suggest you get rid of the body onload attribute and create a function in the <head> section like this:
    Code:
    window.onload = function() {
    formatStr(document.getElementById('tdRef1'),40);
    formatStr(document.getElementById('tdRef2'),40);
    formatStr(document.getElementById('tdRef3'),40);
    // etc
    }
    i jus tried it an it worked...thank you for ur help


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •