0

I am using jquery inside the javascript function to hide & show div. I need to show only div "Area" while hide other div

This one works, when i directly put the name of the div to hide & show :---

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>

<script>

function area_visible()
{


  $('.Area').show();
  $('.Area-1').hide();
  $('.Area-2').hide();
  $('.Area-3').hide();


}

This one does not works if i try to access using array of div class, even alert message is not displayed 4 times for loop :----

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>

<script>

var area_id = [
    "Area" , "Area-1", "Area-2", "Area-3"
];



function area_visible()
{

  $(area_id).each(function(index, element) {
        if(element != area_id[0] )
        {
          $("#" + element).hide();
        }
      alert('11');
    });


}

Please suggest. How to hide and show div by taking there name from an array (and i want to use jquery inside javascript function) ?

2
  • 1
    you have not called your function anywhere. Commented Apr 11, 2014 at 12:21
  • in the first example you are addressing them as classes, and in the second you are addressing them as though they are element IDs. Which are they? Commented Apr 11, 2014 at 12:22

3 Answers 3

3

Change

$("#" + element).hide();

to

$("." + element).hide();

You are trying to target an ID you must use . to target class.

Sign up to request clarification or add additional context in comments.

2 Comments

I saw the first answer and was thoroughly confused :p
@RGraham haha i confused myself for a second there too so i deleted it for a second ^^
1

in your first function you are using class $('.Area-1') and the second function you are selecting with ID $("#"+element)

so the fix is easy just change '#' to '.' in the second function

Comments

0

You have to change your id selector to a class selector and initiate the function:

$("." + element).hide(); // for your second function.

and initialize your func:

area_visible(); // for both it will work.

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.