1

First of all, my intention is to have 1 js file where there are multiple ajax calls. I want these ajax calls, which are php files, to have the same js file inside of it but not doing another request, which makes it to run slow after any click.

Some of my main php file:

<head>
    <script src="<?php echo $_SESSION['url'] ?>js/direcinfo.js"></script>
</head>
<body>

    <div class="direcciones">
        <a href="javascript:void(0)" id="c_menu_direcciones" class="c-menu c_menu_direcciones">
            <p>
              Direcciones
            </p>
        </a>
    </div>
    <div id="cuenta_menu_direcciones" class="c-menu-content">
        <h1>Direcciones de correo</h1>
        <div id="expand_wrapper_dir">
        </div>
    </div>
</body>

Js file (direcinfo.js):

$('#c_menu_direcciones').click(function(){
   $.ajax({
      dataType: "html",
      type: "POST",
      url: "direcalias.php",
      success: function(cntnt){
      $("#expand_wrapper_dir").html(cntnt);
    }
    });
    return false;
});

$(".alias-dir-a").click(function(){
    var useremail=$("#useremail").val();
    var alias=$(this).html();
    if(alias==="+")
    {
        $.ajax({
            dataType: "html",
            type: "POST",
            url: "direcnew.php",
            data: "email="+useremail,
            success: function(cntnt){
                $("#direc-content").html(cntnt);
            }
        });
        return false;
    }
});

Ajax loaded file (direcalias.php)

<?php
    session_start();
    header("Content-Type: text/html; charset=ISO-8859-1");
    include_once 'connection.php';
    $conn = bbddconnect();
    $email=$_SESSION['useremail'];
    $query = "SELECT CODIDIR,ALIAS"
            . " FROM CLIENTE C,DIRECCION D"
            . " WHERE EMAIL LIKE '$email' AND C.CODICNT = D.CODICNT;"; 

    $result3 = mysqli_query($conn,$query)or die(mysqli_error());
    $recount = mysqli_num_rows($result3);
    echo '<div class="menu-alias madir">';
        for($i=0;$i<$recount;$i++)
        {
        $row = mysqli_fetch_array($result3);

    echo '<div class="alias adir ali-'.$i.'">
        <a href="javascript:void(0)" class="alias-dir-a">'.$row['ALIAS'].'</a>
    </div>';

        }

    echo '<div class="alias adir ali-nuevo">
        <a href="javascript:void(0)" class="alias-dir-a">+</a>
    </div>
    </div>
    <div id="direc-content"></div>';

    //echo '<script>'.
    //            'var url = "'.$_SESSION['url'].'js/direcinfo.js";'.
    //            '$.getScript(url);'.
    //    '</script>';

?>

The problem I have is that when the direcalias.php file is called I need to call the js file again (edited part) because if I don't, it does not recognize when I click on $(".alias-dir-a").click(function()). Is what I want to do possible?

2
  • 2
    You can try $("#direc-content").on('click', ".alias-dir-a", function(){ Commented Feb 3, 2016 at 12:03
  • Thank you for your answer, indeed that was what I was looking for. Commented Feb 3, 2016 at 14:20

1 Answer 1

1

It beacuse you call .alias-dir-a before it create. you can use on method

$(document).on("click", ".alias-dir-a", function() {

    var useremail=$("#useremail").val();
    var alias=$(this).html();
    if(alias==="+")
    {
        $.ajax({
            dataType: "html",
            type: "POST",
            url: "direcnew.php",
            data: "email="+useremail,
            success: function(cntnt){
                $("#direc-content").html(cntnt);
            }
        });
        return false;
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Works perfect. Thank you

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.