Changing style of a div

Asked

Viewed 618 times

2

I have a div that stays hidden on the screen:

 <div id="selectCity" name="selectCity" 
    class="form-group col-md-12" style="display: none;" >

    <label for="cities">Para os usuários da cidade:</label>
    <?php echo $this->formRow($form->get('cities')); ?>

 </div>

When changing the radio group selection it should be visible. For this I use this code:

function handleClick(myRadio) {
   if (myRadio.value.localeCompare("selecteduser") === 0) { 
       document.getElementsById("selectCity").style.display = "block";
   } else {
       document.getElementsById("selectCity").style.display = "none";
   }
}

handlerClick is called, the check is done without major problems. The problem is the div style is not changed.

Can anyone tell me why?

  • One @franM question, can’t you use jQuery? I think it’s much easier to work.

  • It could be jQuery

  • I put in the answer I had misunderstood but already found a problem

  • Good afternoon, don’t use irrelevant tags for the question, use tags about the problem and not about the project. I’m sure you’ll take my comment as a constructive criticism.

2 answers

2


Below is an example, and when id is not used getElementsById uses getElementById

function handleClick(myRadio) {
   if (myRadio.value.localeCompare("selecteduser") === 0) { 
       document.getElementById("selectCity").style.display = "block";
   } else {
       document.getElementById("selectCity").style.display = "none";
   }
}

1

Using jQuery

On the radio that will perform the show action

$("#idDoRadio").on("click", function(){
   $("#idDaDiv").show();
});

And on the radio you will perform the occult action

$("#idDoRadio").on("click", function(){
   $("#idDaDiv").hide();
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.