2

I have a javascript function that looks like that:

function myfun(){
//product.1
var t1=document.getElementById('1').innerHTML;
var link = document.getElementsByClassName(t1);

if(document.getElementsByClassName(t1).length==1){
document.getElementById(t1).innerHTML=link[0].outerHTML;
document.getElementById(t1).getElementsByTagName('a')[0].className='dsad';

}
if(document.getElementsByClassName(t1).length==2){
document.getElementById(t1).innerHTML=link[0].outerHTML+'; '+link[1].outerHTML;
var element = document.getElementById(t1).getElementsByTagName('a')[0].className='dsad';
var element2 = document.getElementById(t1).getElementsByTagName('a')[1].className='dsad';
}...
//product.2
var t2=document.getElementById('2').innerHTML;

It goes like that till if(document.getElementsByClassName(t1).length==10) and then it continues element - document.getElementById('2') and so on until it reach Element number 10. The whole script is about 700 lines and I want to reduce it somehow. I was thinking of a for loop but I don't see how I could implement this. Any suggestions?

7
  • What about passing the element's id attribute to a function and then calling that function over and over within a loop? Commented Apr 10, 2014 at 14:00
  • have you ever heard about the for loop? Commented Apr 10, 2014 at 14:02
  • I don't usually link to W3Schools, but the example they use for a for loop is exactly what you're looking for. w3schools.com/js/js_loop_for.asp Commented Apr 10, 2014 at 14:03
  • 1
    In addition to what you already show us, it would be good to have an idea of the HTML structure you have. Maybe there's even a simple way to select things. By the way, once you have done var link = document.getElementsByClassName(t1); , you can then refer to that element using the variable name link , so in the next line you can write : if(link.length==1){ Commented Apr 10, 2014 at 14:04
  • I am puzzled you use t1 both for lookup by class and id. Commented Apr 10, 2014 at 14:08

4 Answers 4

5

Try:

for( i = 1; i <= 10; i++ ){  
  if( document.getElementsByClassName( t1 ).length == i ){
    document.getElementById( t1 ).innerHTML=link[i-1].outerHTML;
    document.getElementById( t1 ).getElementsByTagName( 'a' )[ i-1 ].className = 'dsad';
  }
}

EDIT:

If you want also the element id to be increased you should try:

for( i = 1; i <= 10; i++ ){  

var t1=document.getElementById(''+i).innerHTML;
var link = document.getElementsByClassName(t1);

      if( document.getElementsByClassName( t1 ).length == i ){
        document.getElementById( t1 ).innerHTML=link[i-1].outerHTML;
        document.getElementById( t1 ).getElementsByTagName( 'a' )[ i-1 ].className = 'dsad';
      }
    }
Sign up to request clarification or add additional context in comments.

8 Comments

the ElementID, t1 in this case, should also increase
@user986959 So increment it
u need 2 loops one nested inside another. One for ids, two for lengths
@user986959 do you have multiple elements carrying the same id ? try to avoid that. and you might be interested in using jquery, a cross-browser js library abstracting away from many browser and dom idiosyncrasies while providing useful additional apis.
If you put the repeating document.getElementById(t1) into a variable, like var el = document.getElementById(t1); and use it subsequently, you get it even smaller.
|
3

You could try this:

for(var id=1;id<=10;id++){
    var t1id=""+id;
    var t1=document.getElementById(t1id).innerHTML;
    var link = document.getElementsByClassName(t1);
    for(var num=1;num<=10;num++){
        if(document.getElementsByClassName(t1).length==num){
             document.getElementById(t1).innerHTML=link[0].outerHTML;
             document.getElementById(t1).getElementsByTagName('a')[0].className='dsad';
        }
    }
}

1 Comment

You shouldn't need the var t1id=""+id; line, but this is the actual (not answering as quickly as possible) answer
3
function myfun(){
    for(i=1;i<=10;i++){
        var ti=document.getElementById(i).innerHTML;
        var link = document.getElementsByClassName("t" + i);

        if(link.length==1){
             link.innerHTML=link[0].outerHTML;
             ti.getElementsByTagName('a')[i - 1].className='dsad';

        }
        else if(link.length==2){
            ti.innerHTML=link[i - 1].outerHTML+'; '+link[i].outerHTML;
            var element = ti.getElementsByTagName('a')[i - 1].className='dsad';
            var element2 = ti.getElementsByTagName('a')[i].className='dsad';
        }
     }
}

2 Comments

Could do with some vague explanation?
well I thnk that what he is trying to do is finding out if there are only one or more than one elements with a single class name, I think that this function is clear enough to point him/her in the right direction
2

I am not sure about one part of your code. So I am going to assume that is different depending on the id.

The first I would do is to create a javascript object with the content that you want to include for your className

var classNameDictionary = { 0 : 'dsad', 1 :'dsad, ...};

Once you have this, then I will implement a for loop as following:

function myFun(numElements){
    for(var i = 0; i < numElements; i++) {
        var t = document.getElementById(''+i).innerHTML;
        var link = document.getElementsByClassName(t);
        document.getElementById(t).innerHTML = "";
        for(var j = 0; j < link.length; j++) {
            document.getElementById(t).innerHTML +=link[j].outerHTML;
            var element = document.getElementById(t).getElementsByTagName('a')[j].className = classNameDictionary[j];
        }

    }
}

I hope it helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.