0

I'm trying to target a button I've created by pressing another button. My code technically works, but due to the size of what I'm doing, I'm trying to put the function of the second button press in another js file. That where I'm running into problems. My code is below.

$("#webCoding").on("click", function() {
if ( $( ".webCodingID" ).length ) {
  return false;
  }
picture.attr("src", "img/webCoding.jpeg");
$(".target").empty();
$(".jumbotron").hide();
var buttonGroup = $('<div role="group" aria-label="...">').addClass('btn-group-vertical center-block')
var buttonPhilosophy =$("<button type='button'>").addClass("btn btn-default btn-lg").append("Design Philosophy")
var buttonStack =$("<button type='button'>").addClass("btn btn-default btn-lg").append("Visit My Stack Overflow Account")
var buttonGithub =$("<button type='button' id='git'>").addClass("btn btn-default btn-lg").append("Look at what I've been doing on Github")
var webCodingID =$("<div>").addClass("trainingID")
       $(buttonGroup).append(buttonPhilosophy).append(buttonGithub).append(buttonStack);
$('.target').append(buttonGroup).append(webCodingID);
// code for pressing created button is below.
 $("#git").on("click", function() {
   prompt("herro");
}); 
});     

but I put this in another file (and get rid of the same code in the original js file), and nothing happens.

 $(document).ready(function(){
$("#git").on("click", function(){
  prompt("jungle boogie");
});

I can't figure out why. The js file is linked to my main page, I just can't get it to recognize buttons created by JS in another file.

Eliezer Wohl
  • 577
  • 1
  • 6
  • 20

1 Answers1

1

You should bind the on to the body like so:

$("body").on("click", "#git", function(){

That should solve your problem I believe :)

Chris
  • 987
  • 6
  • 24