0

I've been messing around with bootstrap, jquery, javascript and html for some time now and I still find it rather difficult considering the way that pages, navigation and etc works. One thing in particular that I've had some trouble finding answers for is the best way/practice to build a page..?

I've tried the following for my page, but I always find something that doesn't quite work as I expect it to.

  • Creating an "empty" (containing empty divs) index page and using javascript to load external pages (Problem: I can't seem to use the id's or classes of the elements in the external page anymore with this approach)
  • Creating a single index.html page containing all my divs and hiding/showing whatever I need to (Problem: Quickly becomes very hard to find around in which are showing/hiding and adding stuff can become quite tricky
  • Creating a single html file for all the "pages" I need (Problem: I can't seem to transfer my javascript vars from one page to another. The vars become empty on loading a new page using href)

Index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title>Course Suggestion Tool</title>

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js" type="text/javascript" charset="utf-8"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js" type="text/javascript" charset="utf-8"></script>
  <script src="js/bootstrap.min.js"></script>
  <script src="js/functions.js"></script>

  <!-- Bootstrap -->
  <link href="css/bootstrap.min.css" rel="stylesheet">
  <!-- Custom CSS -->
  <link href="css/custom_style.css" rel="stylesheet">

</head>
<body class="ui-mobile-viewport ui-overlay-c">

<div class="container" id="first">
  <div id="study_area">
    <fieldset>
    <legend>Study</legend>
    <div class="dropdown" id="study_dropdown">
      <button id="study" class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Select Study
        <span class="caret"></span>
      </button>
      <ul class="dropdown-menu">
        <li><a href="#" class="dme" data-value="action-1" id="dme">Digital Media Engineering</a>
        </li>
      </ul>
    </div>
    <div>
      <h4 id="study_choice">Study choice</h4>
    </div>
  </fieldset>
  </div>

  <div id="focus_area">
    <fieldset>
      <legend>Focus Area</legend>
      <div class="dropdown" id="focus_dropdown">
        <button id="study" class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Select Focus
          <span class="caret"></span>
        </button>
        <ul class="dropdown-menu">
          <li><a href="#" id="smaa">Social media and apps</a></li>
          <li><a href="#" id="uxue">UX User experience</a></li>
          <li><a href="#" id="ds">Data science</a></li>
          <li><a href="#" id="cga">Computer games</a></li>
          <li><a href="#" id="cgr">Computer graphics</a></li>
        </ul>
      </div>
      <h4 id="focus_choice">Focus Choice</h4>
    </fieldset>
  </div>

  <button id="next_page" type="button" class="btn btn-primary">Something exciting</button>
</div>

<div class="container" id="second">

</div>

</body>
</html>

NB

The content consists of two dropdowns. On the page load I only want to show the first one. Then once the user selects something from that dropdown I want to show which he selected in a heading and show the second dropdown as well. Once the second one has been selected too I want to display the selection in a heading and show a button that "navigates the user to a new page". However what's really import is that I want to "save" the name of the dropdowns that the user selected to use as some sort of feedback.´

Functions.js

var selection = {};

$(document).ready(function(){
  $("#focus_area").addClass("collapse");
  $("#study_choice").hide();
  $("#focus_choice").hide();
  $("#next_page").hide();

  $(".dme").click(function() {
    $("#study_dropdown").hide()
    var choiceText = $("#dme").text()
    selection.study = choiceText;
    $("#study_choice").text(choiceText)
    $("#study_choice").show()
    $("#focus_area").slideDown();
  });

  $("#smaa").click(function() {
    $("#focus_dropdown").hide();
    var choiceText = $("#smaa").text();
    selection.focus = choiceText;
    $("#focus_choice").text(choiceText);
    $("#focus_choice").show();
    $("#next_page").slideDown();
  });

  $("#uxue").click(function(){
    $("#focus_dropdown").hide();
    var choiceText = $("#uxue").text();
    selection.focus = choiceText;
    $("#focus_choice").text(choiceText);
    $("#focus_choice").show();
    $("#next_page").slideDown();
  });

  $("#ds").click(function(){
    $("#focus_dropdown").hide();
    var choiceText = $("#ds").text();
    selection.focus = choiceText;
    $("#focus_choice").text(choiceText);
    $("#focus_choice").show();
    $("#next_page").slideDown();
  });

  $("#cga").click(function(){
    $("#focus_dropdown").hide();
    var choiceText = $("#cga").text();
    selection.focus = choiceText;
    $("#focus_choice").text(choiceText);
    $("#focus_choice").show();
    $("#next_page").slideDown();
  });

  $("#cgr").click(function(){
    $("#focus_dropdown").hide();
    var choiceText = $("#cgr").text();
    selection.focus = choiceText;
    console.log(selection);
    $("#focus_choice").text(choiceText);
    $("#focus_choice").show();
    $("#next_page").slideDown();
  });

  $("#next_page").click(function() {
    $("#first").load();
    $("#second").load("planner.html") //External html page (currently empty)
  });

});

Zeliax
  • 4,987
  • 10
  • 51
  • 79
  • have you written any javascript code? – Santosh Ram Kunjir Mar 09 '16 at 16:48
  • Take a look at http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements that will explain how to fix the issue of not being able to access things when loaded dynamically. – Wesley Smith Mar 09 '16 at 16:48
  • 1
    Need to scale this down to more specific issues and provide code that isn't working for the specific problem – charlietfl Mar 09 '16 at 16:49
  • 2
    But more importantly, you really need to research general web design and functionality before you start coding. No offense meant but your questions demonstrate a lack of fundamental understanding of how web pages and JavaScript work – Wesley Smith Mar 09 '16 at 16:50
  • @SRK Yes. I have written javascript code to hide and show the elements. – Zeliax Mar 09 '16 at 17:03
  • please share to go ahead – Santosh Ram Kunjir Mar 09 '16 at 17:04
  • @SRK Done.. Updated the entire html code too to illustrate what I'm trying to do.. – Zeliax Mar 09 '16 at 17:29
  • [I would start here to learn more about this topic](https://www.codeschool.com/learn/javascript). They have very good introductions on not just how, but also the why. – trenthaynes Mar 09 '16 at 17:34
  • I can also add [here](http://tutorialzine.com/2015/02/single-page-app-without-a-framework/) and [here](https://msdn.microsoft.com/en-us/magazine/dn463786.aspx) and [here](http://www.awwwards.com/practical-uses-of-angularjs-create-a-single-page-application-spa-or-a-website-menu-in-an-instant.html) and [here](http://singlepageappbook.com/single-page.html) - depending on what your flavor is. – trenthaynes Mar 09 '16 at 17:48
  • Thanks for the suggestions. Currently it looks like I'm leaning towards using AngularJS. I am still uncertain as to what the best practice for developing a web page is, as I didn't get any response. Currently I have one page that I just plan on showing and hiding stuff in it seems. I doubt however that this will be very "pretty" in the long run.. – Zeliax Mar 09 '16 at 18:14

1 Answers1

1

It seems like what you want to create is a Single Page Application.

Your high level requirements will be:

  1. Loading and destroying parts of pages (aka modules) dynamically.
  2. State transitions to cover various use cases.
  3. Dynamically bind and update the state based on user input like click.

While you can surely code your own solution using jQuery, Bootstrap etc I would highly recommend using a MV* framework for creating such a single page application. There are many popular ones like Angular, Ember, Backbone etc that make the process much more easier. React can also be a good view component.

These frameworks provide many features that will make the task of creating dynamic web applications easier.

I know this is not the exact answer you were looking for, but I hope this answer could guide you towards the right approach that will be easier to implement and maintain in the long term.

TeaCoder
  • 1,958
  • 13
  • 13
  • Thanks. When I wrote the above question I didn't have much experience in Web programming. However I do now. And yes. The above suggestion is probably the best way to make a proper web page. I haven't tried all the solutions, but I can certainly say that ASP.Net (MVC) is a good approach. And same with Angular, however I find ASP.Net to be the most "noob" friendly. And with the "new" .Net Core Framework, Microsoft has come out with a pretty good "tutorial" for creating more custom features/functionality. – Zeliax Nov 13 '17 at 07:50