2

My code is essentially this:

<a href="javascript:void(0);" onClick="viewServices(1)">Services</a>
<a href="javascript:void(0);" onClick="viewServices(43)">Services</a>
<a href="javascript:void(0);" onClick="viewServices(16)">Services</a>
...

Where viewServices:

function viewServices(stylistID){
    alert(stylistID);
}

Only, there are two problems:

  1. I am using inline Javascript, and that is discouraged.
  2. When I click a link, the function isn't called.

It should also be mentioned that I am importing JQuery and Bootstraps .js files, and that each link (as well as associated ID) is loaded dynamically.

Is there an established method that will allow me to remove my inline javascript, but also make it so that I can call functions that pass the dynamically loaded link's "ID" to the function?

** EDIT

Each link is generated during a mysqli_fetch_row that repeats until each row's information has been run through. The number in viewServices(#) is actually

<?php echo $row[0]; ?>
2
  • why did you use href="javascript:void(0);" ? Commented Nov 9, 2013 at 3:16
  • I think href="#" would make it better Commented Nov 9, 2013 at 3:17

3 Answers 3

6

A solution is to utilize data attribute and class to refactor html like this

<a href="#" data-service="16" class="service-link">Services</a>

Then, jQuery

$('.service-link').on('click', function(e){
  e.preventDefault(); // Stop default behavior of clicking link
  alert(this.data('service')); // Retrieve the service id and use it
});
Sign up to request clarification or add additional context in comments.

15 Comments

yeap... since HTML5 is not a REAL implementation of SGML, data-attributes are possible... once they couldn't be validated against a DTD.
@DanielBrown, yes data set is in HTML but actually not new :) It's widely accepted in almost all broswers. caniuse.com/dataset
4 upvotes and no nones saw that this.data(service) should be this.data('service')...?
@DanielBrown, my pleasure :) on() listens to the events on the selector, no matter it exists or will be added later dynamically. api.jquery.com/on
@DanielBrown, Sure $('#links-container').on('click', '.service-link', ...); This will take advantage of event bubbling, since click events are listened on the parent element. jQuery will automatically filter out click events that aren't coming from .service-link elements.
|
2

You could use

<a class="viewServices" data-arg="1">Services</a>
<a class="viewServices" data-arg="43">Services</a>
<a class="viewServices" data-arg="16">Services</a>

and

// viewServices is not defined here
(function() { // viewServices is defined inside here
    function viewServices(arg) { /* ... */ }
    function viewServicesCaller() {
        viewServices(this.getAttribute('data-arg'));
    }
    var els = document.getElementsByClassName('viewServices');
    for(var i=els.length-1; i>=0; --i) {
        els[i].onclick = viewServicesCaller;
    }
})();

Probably, your code doesn't work because viewServices is defined inside a closure, so it isn't global. Then, you must add the event listeners in that closure.

Comments

1

What errors are you getting in the console?

Is the javascript in the head section of your page? If not, try that.

If you don't want to do that, make sure the javascript code is before the link.

Good luck.

For jQuery, you could do this:

<a class="services" data-service="1">Services</a>
<a class="services" data-service="43">Services</a>
<a class="services" data-service="16">Services</a>


$(".services").click(function(link){
  link.preventDefault();
  alert(this.data("service-id"));
});

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.