Javascript get existing element problem



  • (moderator's note: moved to "Coding Related Help & Questions")

    Hi , i am newbie in javascript i got a problem to check whether a element exist or not in a form. first , i apologize for my poor language. here is my html form

    ....
    <body>
    <form name="f">
    <table>
    <tr>
    <td><a href="#" onclick="addItem()">more</a></td>
    <td><input type="text" id="Person/Personal/Email[1]</br>
    <input type="text" id="Person/Personal/Email[2]</br>
    <input type="text" id="Person/Personal/Email[3]</br></td>
    </tr>
    <tr>
    <td><a href="#" onclick="addGroup()">more</a></td>
    <td>
    <table Id="Person/Personal/Academic">
    <tr><td>
    <select id="Person/Personal/Academic[1]/School">
    <option>
    ....
    ...
    </select><br>
    <input id="Person/Personal/Academic[1]/Grade">
    </td></tr>
    <tr><td>
    <select id="Person/Personal/Academic[2]/School">
    <option>
    ....
    ...
    </select><br>
    <input id="Person/Personal/Academic[2]/Grade">
    </td></tr>
    </table>
    </td>
    </tr>
    </table>
    </body>

    i got a function called "CheckElementId()". In this function, i will be given a string , let say "Person/Personal/Academic/School" now, my problem is how to i determine where is the index. i mean is the index is after the "school" so the element id is like :"Person/Personal/Academic/School[1]" or after the Academic like "Person/Personal/Academic[1]/School" . I already try my best think and think again a whole days. Guys, i fade up , so really need javascript expert to help me. at least give some ideal... THANKS A LOT



  • This is the wrong forum. Use the other board "Coding related help & questions" for things like this.

    But just for you, the indexOf function asks whether a string is in another string, if that is what you want:

    <font face="Courier New">"Person/Personal/Academic[1]/Grade".indexOf("[1]") <font face="Times New Roman">is 24 because "[1]" occurs at the 24th character.
    </font>"Person/Personal/Academic[2]/Grade".indexOf("[1]") <font face="Times New Roman">is -1 because "[1]" doesn't occur in the string.
    So if you want to check if there's a "[1]" in a string, use<font face="Courier New"> "</font></font>Person/Personal/Email[1]</font><font face="Courier New"><font face="Times New Roman"><font face="Courier New">".indexOf("[1]") == -1</font>
    </font></font>



  • Even though i don't understand what you are trying to do, i guess the best way would be to use string.split("/"). This returns an array where you just look at every item if it ends with an id (e.g. with string.match(...)).

    But that are just random thoughts. It could be helpfull if you would elaborate a bit more on what you are trying to achieve (e.g. why don't you know which elements exist in your form?).



  • Just to get this straight,

    You have a function called [code]CheckElementId(idString)[/code]. The argument is the id.

    For instance, the id that you pass can be:

    "Person/Personal/Academic/School[1]"
    or
    "Person/Personal/Academic[1]/School"

    And you want to know where, inside the string, the "[1]" part is?
    That depends on what you mean by "where". You can get the index number of the location of "[1]" in a string like so:

    [code]var indexNumber = idString.indexOf('[1]')[/code]

    However, I assume that you want to know two things:
    - the index number itself, [1] or [2] or [3]
    - what word it is next to, Academic or School or Personal.

    Then it becomes a bit more complex:

    //put all "/"-separated sections in an array
    var idLevels = idString.split('/');
    
    //a small regex that looks for: "[" "number" "]"
    var regIndex = /\[\d\]/;
    
    //the final value you want
    var indexLevel = '';
    
    //loop over the levels
    for (var i=0; i < idLevels.length; i++)
    {
       //if the level has "[number]" in it
       if (regIndex.test(idLevels[i]))
       {
          //put the level and its number in indexLevel
          indexLevel = idLevels[i];
          //stop the loop; you don't need to search further
          break;
       }
    }
    
    //END
    //the variable indexLevel will now contain something like "Personal[1]" or "School[1]"
    

    Does that help?


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.