Javascript/jquery for multiple ID's

Junior Spellweaver
Joined
Aug 15, 2011
Messages
167
Reaction score
15
Hea there,

I have a question. I want to make a minimail for my CMS and every mail must show his content when you click on it. Like this:
Galago - Javascript/jquery for multiple ID's - RaGEZONE Forums


And the javascript I am using:

PHP:
	<script language="javascript"> 
$(document).ready(function(){
    $('#minimail').toggle(
       function() {
          
          $('#minimailinhoud').toggle();
          return false;
       },
       function() {
          
          $('#minimailinhoud').toggle();
          return false;
       }
    );
});
</script>

And the html:
PHP:
<td width="500" class="minimail">
                <p class="inboxnummer">1</p>
						    <p class="inbox" id="minimail" onclick="javascript:toggle();">Mailtje</p>
						    <p class="minimailinhoud" id="minimailinhoud" style="display: none; margin-right:10px;">Afzender:</p>
							</td>

Is it possible to use a jqyery/javascript thingy for every mail I get that it does the function that it toggles?

I hope I have explained enough. If not, tell me.

Yours faithfully,

Galago
 
I don't understand the question? Why don't you try it.

Also it's obviously possible as you've seen it in google mail , yahoo and some other stuff. I do not know if it's the same language used but I have done it before but you are on the right path.

Each element would also be assigned to their own ID or DIV rather than <p> or <div>. Then jq or js comes in and does all the magic.
 
I'm on phone so this will be sketchy.
Code:
$('title').click(function(){
     $(this).parent().find('body').slideToggle();
})

Where structure is
Code:
Div
     Div with message title
     Div with body
/div

Of course variables I've put in js are as sketch do not take them literally - to lazy to fix it over phone
 
Last edited:
Wrap all of your messages in a separate div with the same id or class. Then register a click handler from within a .each().

PHP:
$(".message").each(function() {
    $(this).click(function() {
        $(this).toggle();
    });
});

Something like that should work, haven't done jQuery in ages.
 
Last edited:
I'm on phone so this will be sketchy.
Code:
$('title').click(function(){
     $(this).parent().find('body').slideToggle();
})

Where structure is
Code:
Div
     Div with message title
     Div with body
/div

Of course variables I've put in js are as sketch do not take them literally - to lazy to fix it over phone
Isn't that for just 1 div? Because when you use that, you need to put that js for like every div. I'm sorry if that isn't true. I am nog a hero with js. ^_^
 
It doesn't work for me. First i tried to edit it, so it works for my own classes and id's, that didn't work. Then I tried to copy the whole code from jsfiddle and put it on my site, but that worked neither. Am I just that stupid?
PHP:
<script>
$('.showmails').on('click', '.inbox', function () {
   $(this).next('.minimailinhoud').toggle(); 
});
$('.showmails .minimailinhoud').hide();
</script>
PHP:
<div class="showmails">
                <p class="inboxnummer">1</p>
						    <p class="inbox">Mailtje</p>
						    <p class="minimailinhoud" style="margin-right:10px;">Afzender:</p>
                </div>
 
Always put your script tags at the end of the body, and you need to include jQuery before your scripts which use jQuery.

If you look at the JavaScript error console, you will see this:
Code:
Uncaught ReferenceError: $ is not defined
$ refers to jQuery. '$ is not defined' means that jQuery was not included before it was referenced- hence the "ReferenceError"


The end of your html should look like this:
Code:
  ...
  <script src="link/to/jQuery/cdn"></script>
  <script src="link/to/my/js"></script>
</body>
</html>
Notice the entire web-page HTML is above the JavaScript. - That causes the web page to load faster, and it also results in the DOM being ready to manipulate without checking readystate or onLoad.
Notice the jQuery library is before the js file which uses jQuery. - That causes '$' and 'jQuery' to be available for future JavaScript code.

You do not need to check if the DOM is ready, and you can rest assured that jQuery is available unless the request for the jQuery file failed.

You can tell if the request failed if you are using a browser that supports an inspector- such as Chrome, Opera or Firebug+Firefox. Right click, inspect element, and click on Network. If you see any entries with a 500, or 404 status codes (to name a couple), or with red text, then there's a problem. Firefox has a built in inspector that wont conform to these instructions. Use "Inspect Element with Firebug" when right-clicking in firefox. If you do not have that option, search for "firebug" and download the firebug add on for firefox.

Edit this Fiddle - jsFiddle
jsFiddle will simulate jQuery and putting the JS at the bottom of the body.
On the left-hand sidebar, there are two select menus. The first is for selecting the placement of the JavaScript code. "no wrap (body)" selection will place the javascript like I described- at the bottom of the HTML code.
The second select menu is for picking the JavaScript library. For this, I selected jQuery 1.9. In your fiddle you did not have a library selected- explaining your ReferenceError for jQuery.
 
Last edited:
Always put your script tags at the end of the body, and you need to include jQuery before your scripts which use jQuery.

If you look at the JavaScript error console, you will see this:
Code:
Uncaught ReferenceError: $ is not defined
$ refers to jQuery. '$ is not defined' means that jQuery was not included before it was referenced- hence the "ReferenceError"


The end of your html should look like this:
Code:
  ...
  <script src="link/to/jQuery/cdn"></script>
  <script src="link/to/my/js"></script>
</body>
</html>
Notice the entire web-page HTML is above the JavaScript. - That causes the web page to load faster, and it also results in the DOM being ready to manipulate without checking readystate or onLoad.
Notice the jQuery library is before the js file which uses jQuery. - That causes '$' and 'jQuery' to be available for future JavaScript code.

You do not need to check if the DOM is ready, and you can rest assured that jQuery is available unless the request for the jQuery file failed.

You can tell if the request failed if you are using a browser that supports an inspector- such as Chrome, Opera or Firebug+Firefox. Right click, inspect element, and click on Network. If you see any entries with a 500, or 404 status codes (to name a couple), or with red text, then there's a problem. Firefox has a built in inspector that wont conform to these instructions. Use "Inspect Element with Firebug" when right-clicking in firefox. If you do not have that option, search for "firebug" and download the firebug add on for firefox.

Edit this Fiddle - jsFiddle
jsFiddle will simulate jQuery and putting the JS at the bottom of the body.
On the left-hand sidebar, there are two select menus. The first is for selecting the placement of the JavaScript code. "no wrap (body)" selection will place the javascript like I described- at the bottom of the HTML code.
The second select menu is for picking the JavaScript library. For this, I selected jQuery 1.9. In your fiddle you did not have a library selected- explaining your ReferenceError for jQuery.
I am sorry, I don't get you. I have my javascript/jquery in my <head></head>. So accualy that isn't a good idea?
 
I am sorry, I don't get you. I have my javascript/jquery in my <head></head>. So accualy that isn't a good idea?

Yes. Javascript takes time to load, if you put all javascript at the bottom of the webpage the entire page will render first making the site look faster than it is.

You should NEVER put any javascript in the header. It's a common mistake people do and once their javascript becomes advanced the site takes longer to load and looks bad.

All the best,
Richard Komakech.
 
Yes. Javascript takes time to load, if you put all javascript at the bottom of the webpage the entire page will render first making the site look faster than it is.

You should NEVER put any javascript in the header. It's a common mistake people do and once their javascript becomes advanced the site takes longer to load and looks bad.

All the best,
Richard Komakech.
So accualy when I put it trough a link in the file, it will work?
 
Back