1

Hi everyone i need get name class menu li for load this page

html

<body>
<div class="menu">
  <ul>
    <li class="page2" id="page2">PAGE TOW</li>
    <li class="page3" id="page3">PAGE THREE</li>
  </ul>
</div>
<div class="load"></div>
</body>

js

$(document).on('click','.menu li',function(){
    var Get_Name_Class = //Get Name Class
    if(/* var Get_Name_Class*/).hasClass("page2")) {
      $(".load").load('page2.php');
    }
    else if(/* var Get_Name_Class*/).hasClass("page3")) {
      $(".load").load('page3.php');
    }
});

how can i this ( get id or class not difference )

1
  • $(this).attr('class') Commented Feb 11, 2017 at 6:10

6 Answers 6

4

Use this to refer the clicked element inside the handler callback.

$(document).on('click','.menu li',function(){
    // cache the reference
    var $this = $(this);
    // check using the cached element
    if($this.hasClass("page2")) {
      $(".load").load('page2.php');
    }
    else if($this.hasClass("page3")) {
      $(".load").load('page3.php');
    }
});
Sign up to request clarification or add additional context in comments.

Comments

3

You can do it using jQuery. If it is class you can do:

$(".className")

if it is id you can do:

$("#idName")

if it is just html element you can do:

$("elementName")

1 Comment

How does Answer get .className of element?
1

Pass this.className with ".php" concatenated to .load()

$(document).on('click','.menu li',function() {
  $(".load").load(this.className + ".php") 
});

Comments

0

$(document).on('click','.menu li',function(){
    // cache the reference
    var $this = $(this);
    // check using the cached element
    if($this.hasClass("page2")) {
      $(".load").load('page2.php');
    }
    else if($this.hasClass("page3")) {
      $(".load").load('page3.php');
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div class="menu">
  <ul>
    <li class="page2" id="page2">PAGE TOW</li>
    <li class="page3" id="page3">PAGE THREE</li>
  </ul>
</div>
<div class="load"></div>
</body>

Comments

0

use "#" symbol for id so your code becomes ("#idname") or u can use "this" which points to the present class u are working on

Comments

0

Don't use classnames. One day you'll add an extra class to those elements and your site will stop working and you'll be left wondering why - or in the worst case it'll slip unnoticed.

Say you have one or even more buttons for page2 triggering - than it's the perfect place to use the data-* attribute!

$(document).on('click', '[data-load]', function() {
  
  var page = $(this).data("load");
  console.log(page); // Just to test
  $(".load").load(page +'.php'); 

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a data-load="page2">PAGE TWO :) me too</a>

<div class="menu">
  <ul>
    <li data-load="page2">PAGE TOW</li>
    <li data-load="page3">PAGE THREE</li>
  </ul>
</div>
<div class="load"></div>

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.