www.webdeveloper.com
Results 1 to 8 of 8

Thread: If content starts with ( change content

  1. #1
    Join Date
    Jan 2010
    Location
    Perth
    Posts
    133

    If content starts with ( change content

    Hello

    I would like to know if I have a cell that contains (12) (the number will be random but will always be enclosed in a bracket) can I use Javascript to find the and replace the td with a - (hyphen) instead.

    Thinking of something along the lines of:

    Code:
    window.load =fandre;
    function fandre(){
    
    var table = document.getElementById("ROSTER");
    var arr = table.getElementsByTagName("td");
    //Finding code here
    
    for (i = 0; i < arr.length; i++) {
    
    status = arr[i].innerHTML;
         if(status == fi) {
    
       arr[i].innerHTML = arr[i].innerHTML = "-";
    
                           }
    }
    
    }

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,330

    Lightbulb Something to try ...

    I'm not sure I understand your 'bracket' description, but you might try this ...
    Code:
    <html>
    <head>
    <title>Cell replacement</title>
    <script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/newreply.php?do=postreply&t=231667
    
    function fandre(unk){
      var table = document.getElementById("ROSTER");
      var arr = table.getElementsByTagName("td");
    //Finding code here
      for (i=0; i<arr.length; i++) {
        status = arr[i].innerHTML;
        if (status == unk) { arr[i].innerHTML = "-"; }
      }
    }
    </script>
    </head>
    <body>
    <div id="ROSTER">
    <script type="text/javascript">
      var str = '<table border="1"><tr>';
      for (var i=0; i<25; i++) {
        str += '<td>'+i+'</td>';
        if ((i % 5) == 4) { str += '</tr><tr>'; }
      }
      str += '</tr></table>'
      document.write(str);
    </script>
    </div>
    <button onclick="fandre('0')">Replace 0</button>
    <button onclick="fandre('6')">Replace 6</button>
    <button onclick="fandre('12')">Replace 12</button>
    <button onclick="fandre('18')">Replace 18</button>
    <button onclick="fandre('24')">Replace 24</button>
    </body>
    <html>
    How close is this to your needs?

  3. #3
    Join Date
    Jan 2010
    Location
    Perth
    Posts
    133
    Close but not close to my needs thanks.

    Another way i guess we could go about it is make an array of the numbers 1 - 25 as thats might random number limit.

    If any of the numbers in the array pops up then if it could rewrite the content in the cell.

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,330

    Arrow Try this ...

    Random table contents? OK.
    Code:
    <html>
    <head>
    <title>Cell replacement</title>
    <script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/newreply.php?do=postreply&t=231667
    
    function fandre(unk){
      var table = document.getElementById("ROSTER");
      var arr = table.getElementsByTagName("td");
    //Finding code here
      for (i=0; i<arr.length; i++) {
        status = arr[i].innerHTML;
        if (status == unk) { arr[i].innerHTML = "-"; }
      }
    }
    function randOrd() {
      return (Math.round(Math.random())-0.5);
    }
    
    </script>
    </head>
    <body>
    <div id="ROSTER">
    <script type="text/javascript">
      var rndArr = [];
      for (var i=1; i<=25; i++) { rndArr.push(i); }
      rndArr.sort(randOrd);
    
      var str = '<table border="1"><tr>';
      for (var i=0; i<rndArr.length; i++) {
        str += '<td>'+rndArr[i]+'</td>';
        if ((i % 5) == 4) { str += '</tr><tr>'; }
      }
      str += '</tr></table>'
      document.write(str);
    </script>
    </div>
    <button onclick="fandre('1')">Replace 1</button>
    <button onclick="fandre('6')">Replace 6</button>
    <button onclick="fandre('12')">Replace 12</button>
    <button onclick="fandre('18')">Replace 18</button>
    <button onclick="fandre('24')">Replace 24</button>
    </body>
    <html>

  5. #5
    Join Date
    Jan 2010
    Location
    Perth
    Posts
    133
    I dont want to have to click a button. I want it to be a on load.

    All I need it to do is if the following numbers appear on this page in a cell I want them to hide it.

    Numbers are:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

    If they appear replace with a -

  6. #6
    Join Date
    Jan 2010
    Location
    Perth
    Posts
    133
    what I also want to make aware is alot of these numbers will show up so what I've done is I've placed them in brackets so if its possible to even just search for the character ( and replace all the contents in that cell that contains the "(" with a -

  7. #7
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,330
    Your post #1 did not give me enough information to solve your problem.
    I have been guessing as to what you want to do.
    What I have provided is a template for you to fill in with your own code and a function to make the changes requested. Can it be tweeked? Sure, but I'm tired of guessing what your requirements are especially if they are modified every other post. Provide at least some HTML code of your own with an attempt to use the provided scripts and we'll see what we can do from there.

  8. #8
    Join Date
    Jan 2010
    Location
    Perth
    Posts
    133
    I pointed out that I wanted it to be a windows onload function with no input.

    I provided plenty of explantions and then even dumbed it down further.

    simply put again:

    All I need it to do is if the following numbers appear on this page in a cell I want them to hide it.

    Numbers are:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

    If they appear replace with a -

    Extra notes - reason to reduce processing time:
    what I also want to make aware is alot of these numbers will show up so what I've done is I've placed them in brackets so if its possible to even just search for the character ( and replace all the contents in that cell that contains the "(" with a -
    Last edited by ericatekka; 06-23-2010 at 12:45 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles