0

I've a line of code like this:

 htmlcontent =  htmlcontent+'<div class="checkbox" id="chkpos[j]">'+optionAlpha+'<label class="lclass"><input type="checkbox" class="checkbox" name="answer" value='+optionid+'>'+option+'</label>'+htmloptionImage+'</div>';  

And ids in external css are like this:

            #chkpos1{
                /*position: relative;*/
                margin-top: 8%;   
            }
            #chkpos2{
                /*position: relative;*/
                margin-top: 26%;   
            }
            #chkpos3{
                /*position: relative;*/
                margin-top: 17%;   
            }  

How can I make that javascript code ('<div class="checkbox" id="chkpos[j]">') access those ids?
Any ideas?

9
  • 1
    I'd avoid using [ and ] characters in the id attribute. Commented Jun 19, 2015 at 10:06
  • What do you mean by using JS to "access those ids"? JS is mostly used to access DOM elements, not to access CSS rules. Please state your question in a clearer way. Commented Jun 19, 2015 at 10:13
  • 1
    htmlcontent = htmlcontent+'<div class="checkbox" id="chkpos'+j+'">'; Commented Jun 19, 2015 at 10:18
  • @Bhuvana: Please check quotes, they're not in order Commented Jun 19, 2015 at 10:19
  • @AjayKulkarni I checked in the console. It works. Commented Jun 19, 2015 at 10:21

3 Answers 3

3

If you want to use Ids in your CSS (though I recommend using classes instead like Cerbrus said) set your Id like this:

htmlcontent =  htmlcontent+'<div class="checkbox" id="chkpos' + j + '">'+optionAlpha+'<label class="lclass"><input type="checkbox" class="checkbox" name="answer" value='+optionid+'>'+option+'</label>'+htmloptionImage+'</div>';

You can't use [ ] brackets in css as Ids as CSS uses these for attributes (such as input[type=text]).

Like I said though, I'd recommend using classes, so in your JS:

htmlcontent =  htmlcontent+'<div class="checkbox chkpos chkpos' + j + '">'+ //etc...

CSS:

.chkpos {
    /*position: relative;*/
}
.chkpos1 {
    margin-top: 8%;   
}
.chkpos2 {
    margin-top: 26%;   
}
.chkpos3 {
    margin-top: 17%;   
} 
Sign up to request clarification or add additional context in comments.

2 Comments

It should be htmlcontent = htmlcontent+'<div class="checkbox chkpos' + j + '">'+ //etc..., right?
I added the extra chkpos class so that you can give common css rules to all of these checkboxes such as in .chkpos { /*position: relative;*/ }
1

[] are invalid characters for element id's (or classes), as they're used in CSS for attributes.

Use classes instead:

htmlcontent = htmlcontent +
    '<div class="checkbox" class="chkpos pos' + j + '">' + 
        optionAlpha +
            '<label class="lclass"><input type="checkbox" class="checkbox" name="answer" value='+optionid+'>' +
                option +
            '</label>' +
        htmloptionImage +
    '</div>'; 

CSS:

.chkpos {
    /*position: relative;*/
}
.chkpos.pos1 {
    margin-top: 8%;   
}
.chkpos.pos2 {
    margin-top: 26%;   
}
.chkpos.pos3 {
    margin-top: 17%;   
} 

1 Comment

class names like .1, .2 .3 are also invalid. A name must begin with an underscore (_), a hyphen (-), or a letter(a–z)
0

avoid [] brackets and i think j use for loop

  htmlcontent =  htmlcontent + "<div class="checkbox" class='chkpos " + j + "'>'+optionAlpha+'<label class="lclass"><input type="checkbox" class="checkbox" name="answer" value='+optionid+'>'+option+'</label>'+htmloptionImage+'</div>";

css

.chkpos {
    /*position: relative;*/
}
.chkpos.1 {
    margin-top: 8%;   
}
.chkpos.2 {
    margin-top: 26%;   
}
.chkpos.3 {
    margin-top: 17%;   
} 

3 Comments

Does ` id='chkpos " + j + '` access .chkpos.1?
yes it access because i use "chkpos " space given between text and j
sorry for i am not change id attribute to class

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.