Find out the Html Tag?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chelvan
    New Member
    • Aug 2008
    • 90

    Find out the Html Tag?

    my javascript variable read the following contents. now i want to find out the tags, with the name of "cv".

    is it possible?

    Code:
    <p><img src="E:11Che05L9V85AnImgSX-MASIMGchristmas_bells.gif" alt="11" width="313" height="313" /><input id="cv" name="cv" type="text" value="E:11Che05L9V85AnImgSX-MASIMGchristmas_bells.gif" /></p>
    
    <p><img src="E:11Che05L9V85AnImgSX-MASIMGchristmas_bells.gif" alt="11" width="313" height="313" /><input id="cv" name="cv" type="text" value="E:11Che05L9V85AnImgSX-MASIMGchristmas_bells.gif" /></p>
    
    <p><img src="E:11Che05L9V85AnImgSX-MASIMGchristmas_bells.gif" alt="11" width="313" height="313" /><input id="cv" name="cv" type="text" value="E:11Che05L9V85AnImgSX-MASIMGchristmas_bells.gif" /></p>
    thanks
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    try getElementsByNa me(). and do correct the IDs (give each element a different ID). they are meant to be unique, otherwise you run into problems.

    Comment

    • RamananKalirajan
      Contributor
      • Mar 2008
      • 608

      #3
      @ Chelvan,
      As Dormilich said Id's are unique. You have given the same id value for all the input variable it should not be done. It will invalidate your page.

      Regards
      Ramanan Kalirajan

      Comment

      • clai83
        New Member
        • Dec 2007
        • 41

        #4
        I have to agree with the previous posts that the code you posted is invalid. All of your input tags contain id attributes with "cv", and ids should be unique. Each variable must be given a unique name as well I think but I don't know the full extent of your project.

        Here is an example using getElementsByNa me
        Code:
        var cvTags   = document.getElementsByName("cv");
        var cvCount = cvTags.length;
        
        for (var i = 0; i < cvCount; i++) {
            
            //do something with each tag with name of cv
            cvTags[i]
        
        }
        Here is an example using the class attribute instead of the name attribute

        Code:
        var inputTags   = document.getElementsByTagName("input");
        var inputCount = inputTags.length;
        
        for (var i = 0; i < inputCount; i++) {
            // to get the class name of a tag us  .className
            // I am using .indexOf to see if cv is in the className string
            // because I do not know if you are going to use multiple classes.
            // if it is then indexOf("cv") should give me a number other than -1
            // if it doesn't exist then it does give me -1 and I move on to the next one
            // in the loop
            if (inputTags[i].className.indexOf("cv") !== -1) {
                   // you have found an <input> tag with class of "cv"
                   // so now you can do something with it.
            }
        }
        Alternatively,

        Code:
        var inputTags   = document.getElementsByTagName("input");
        var inputCount = inputTags.length;
        
        for (var i = 0; i < inputCount; i++) {
            // if you are using only 1 class then you can also do this
            if (inputTags[i].className === "cv") {
                   // you have found an <input> tag with class of "cv"
                   // so now you can do something with it.
            }
        }
        There are more specific ways to search for tags with specific attributes, but this should get you started. If you are looking for a more specific way then please give more details.
        Last edited by clai83; Nov 29 '09, 10:09 AM. Reason: re-thought answer

        Comment

        Working...