javascript - How to style div with jQuery? -


  <c:foreach items="${list}" var="item">     <div class="block" id="${item.id}" style="background: black"></div> </c:foreach> 

is possible style div depends on current item.id avoiding jsp <c:choose> <c:when test="${ item.num == 0}"> inside div when first load page?

item.num can 1 or 0.

need this:

 if(item.num==0){     style="background: black"     }else{     style="background: transparant"     } 

why not use ternary operator? no need jquery here...

<c:foreach items="${list}" var="item">     <div class="block" id="${item.id}" style="background: ${item.num==0?'black':'white'}"></div> </c:foreach> 

update

if want correctly, try avoid inline styles , add css class instead. can define styles in separate file.

<c:foreach items="${list}" var="item">     <div class="block block-${item.num==0?'black':'white'}" id="${item.id}"></div> </c:foreach> 

and corresponding css file.

/** myapp.css */ .block-black {     background: black; } .block-white {     background: white; } 

update 2

another solution save result variable.

<%-- save result variable (${itembackground}) better reusability --> <c:foreach items="${list}" var="item">     <c:set var="itembackground" value="${item.num==0?'black':'white'}">     <div class="block ${itembackground}" id="${item.id}"></div> </c:foreach> 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -