1

Hello i've been trying to change the content of a div using jquery and i have the following problem, this is my code:

<div id="pages">
   <div id='cssmenu'>
   <ul id="themenu">
   <li class='active'><a href='#'><span>Home</span></a></li>
   <li class=''><a href='#'><span>Modules</span></a></li>
   <li class=''><a href='#'><span>Degrees</span></a></li>
   <li class=''><a href='#'><span>About us</span></a></li>
 </ul>

In the js.js :

var Modules = "<p> 12346 </p>";
var Degrees = "<p> degrees </p>";

$(document).ready(function() {
$('ul#themenu li').click(function() {
    $('li.active').removeClass('active');
    $(this).addClass('active');
    **$('#content').html(Modules);**
});

What I am trying to do is have the content changes accordingly with the variable that matches the ul's list items span name, if you can think of a better way to do this please let me know, thank you in advance, Chris.

0

4 Answers 4

2

If I understood you correctly, you are trying to use the name of the menu item to determine which content to use. I would save it in an object like this:

var text = {
   Modules : "<p> 12346 </p>",
   Degrees : "<p> degrees </p>"
};

and then for the click:


$('ul#themenu li').click(function() {
    var section = $(this).find("span").text();
    $('li.active').removeClass('active');
    $(this).addClass('active');
    $('#content').html(text[section]);
});

FYI, it would probably be cleaner not to use the .text() but rather to add an ID or class to the li and key off of that.

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

Comments

1

I believe you need to map your JS data to your HTML elements somehow. An easy way is to attach a class or an ID to your elements so you can access it directly, Right now it would include getting the text of your span tag then accessing your global data using that value

Using current model in your on click function

var key = $(this).find('span').text(); // Modules
window[key] // var Modules = "<p> 12346 </p>"

Using id and storing data in object:

<div id="pages">
   <div id='cssmenu'>
   <ul id="themenu">
   <li class='active'><a href='#'><span>Home</span></a></li>
   <li id="Modules" class=''><a href='#'><span>Modules</span></a></li>
   <li class=''><a href='#'><span>Degrees</span></a></li>
   <li class=''><a href='#'><span>About us</span></a></li>
 </ul>

then you can easily access modules using the id $('#Modules')

If you need to make handling clicks completely generic you could create an object and have properties corresponding to the elements id

var DATA = {
  'Modules': 'darfdsa';
}

HThen on click

DATA[$(this).attr('id')];

Comments

1

See output by clicking last tree links ,You can do something like this

var Modules = "<p style='color:red;'> 12346 </p>";
var Degrees = "<p style='color:green;'> degrees </p>";
var Aboutus = "<p style='color:yellow;'> About us</p>";

$(document).ready(function() {
$('ul#themenu li').click(function() {
    $('li.active').removeClass('active');
    $(this).addClass('active');
  //alert($(this).find('span').text().replace(/\s+/g, ""));
  $('#content').html(window[$(this).find('span').text().replace(/\s+/g, "")]);
});
  });
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pages">
   <div id='cssmenu'>
   <ul id="themenu">
   <li class='active'><a href='#'><span>Home</span></a></li>
   <li class=''><a href='#'><span>Modules</span></a></li>
   <li class=''><a href='#'><span>Degrees</span></a></li>
   <li class=''><a href='#'><span>About us</span></a></li>
 </ul>
     <div id="content"></div>

Comments

1

Well just try:

$('#content').html(Modules);

Use the HTML DOM innerHTML Property to change elements content.

Or just using javascript and getElementById property:

document.getElementById("content").innerHTML=Modules;

1 Comment

Your first line of code is invalid as jQuery objects have no innerHTML property.

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.