spetiwala spetiwala - 3 months ago 7
jQuery Question

How do use the navbar to trigger an event?

I have been trying for days.
What i want to do is use the options in the navigation menu that i have to trigger onload event in java script/jquery. However, the events just doesnt fire up. I have tried using onselect and onclick as well but with no avail.
The event should bring data into my template.
Maybe im making a mistake that i just cant see. or is there another better way to do this?

This is my Navigation bar. You can see the onselect functions that i was trying to use.

<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">MEK Steel</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li>
<a href="home.html">Home</a>
</li>
<li class="dropdown" >
<a href="#" class="dropdown-toggle" data-toggle="dropdown">About <b class="caret"></b></a>
<ul class="dropdown-menu">
<li>
<a href="about.html#about">About Us</a>
</li>
<li>
<a href="about.html#history">History</a>
</li>
<li>
<a href="about.html#privacyPolicy" >Privacy Policy</a>
</li>
<li>
<a href="about.html#termsAndConditions">Terms and Conditions</a>
</li>
<li>
<a href="about.html#disclaimer">Disclaimer</a>
</li>
</ul>
</li>
<li class="dropdown" class="active">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Manufactured Products <b class="caret"></b></a>
<ul class="dropdown-menu">
<li onselect = "steelProducts()">
<a href="steelProducts.html">Steel Products</a>
</li>
<li onselect = "steelDoors()">
<a href="steelDoors.html">Steel Doors</a>
</li>
<li onselect="digitalSafes()">
<a href="digitalSafes.html">Digital Safes</a>
</li >
<li onselect="securityEq()">
<a href="securityEquipment.html">Security Equipment</a>
</li>
<li onselect="storageSol()">
<a href="storageSolutions.html">Storage Solutions</a>
</li>
<li onselect="steelFur()">
<a href="steelFurniture.html">Steel Furniture</a>
</li>
</ul>
</li>
<li>
<a href="services.html">Services</a>
</li>
<li>
<a href="contact.html">Contact</a>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>


This is my javascript. i have only written one for testing purposes

function showTemplate(template, data){
var html = template(data);
console.log(html);
$('#content').html(html);
}
function securityEq(){
source = $("#productsTemplate").html();
secEqTemp = Handlebars.compile(source);
showTemplate(secEqTemp, data);
}

Answer

After you explained what you were trying to do

@Chax im trying for the onselect to work before the a tag does. like i want to load data and put it in my template and then show the page.

I came up with this code

$(document).ready(function(){
  $('a').click(function(){ // Bind a click event to my <a> tag
    var linkValue = $(this).attr('data-link'); // Retreive the value of data-link aka where we want to go
    // Do something, your logic
    
    
    window.location.href = linkValue; // Load the new page
    });
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a data-link="http://www.google.com">I'm a link</a>
<!-- Notice that the tag below doesn't have a href attribute but does have a data-link attribute-->

What this code does is simple. We bind a click event onto every <a> tag in the DOM. When one of our tag is clicked, we execute the function. Note that you can differenciate button using a switch. Then we call the window object and make it call the call the page we want.

Comments