0

I was following a tutorial (which I modified a bit) here: code

In JSFiddle the code is working neat, however, on the real page which code is below, is not working, I've been struggling but I cannot find an answer :-( so any help is appreciated.

<html>

<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
    <script type="text/javascript" src="javascript/jquery-2.1.0.min.js"></script>
    <script type="text/javascript">

        $('.multi-field-wrapper').each(function() {
            var $wrapper = $('.multi-fields', this);
            $(".add-field", $(this)).click(function(e) {
                $('.multi-field:first-child', $wrapper).clone(true).appendTo($wrapper).find('input').val('').focus();
            });
            $('.multi-field .remove-field', $wrapper).click(function() {
                if ($('.multi-field', $wrapper).length > 1)
                    $(this).parent('.multi-field').remove();
            });
        });

    </script>
    <title>AQUATAP - Gestor de Información - Añadir Pedido</title>
</head>

<body>

    <form role="form" action="add_order.php" method="POST">
        Cliente:
        <input type="text" name="cliente" id="buscar">
        <br>
        <hr />
        Fecha de salida:
        <br>
        <input type="radio" name="salida_pronosticada" value="male">
        En el día
        <br>
        <input type="radio" name="salida_pronosticada" value="male">
        2 días
        <br>
        <input type="radio" name="salida_pronosticada" value="female">
        3 días
        <br>
        <input type="radio" name="salida_pronosticada" value="female">
        5 días
        <br>
        <input type="radio" name="salida_pronosticada" value="female">
        1 semana
        <br>
        <input type="radio" name="salida_pronosticada" value="female">
        Otro
        <input type="text" name="salida_pronosticada_otro">
        días
        <br>
        <hr />
        <label>Stuff y cantidad</label>
        <div class="multi-field-wrapper">
            <div class="multi-fields">
                <div class="multi-field">
                    <input type="text" class="buscar_prod" name="input_referencia[]">
                    <input type="text" name="input_cantidad[]">
                    <button type="button" class="remove-field">
                        X
                    </button>
                </div>
            </div>
            <button type="button" class="add-field">
                Add field
            </button>
            <input type="submit" name="guardar" value="Guardar" />
        </div>
    </form>

</body>

So well... any help is appreciated. I've been playing around and I think i might be missing something... like if the script didn't start.

4 Answers 4

2

Add Your Code Inside document.ready

$(function(){
$('.multi-field-wrapper').each(function() {
        var $wrapper = $('.multi-fields', this);
        $(".add-field", $(this)).click(function(e) {
            $('.multi-field:first-child', $wrapper).clone(true).appendTo($wrapper).find('input').val('').focus();
        });
        $('.multi-field .remove-field', $wrapper).click(function() {
            if ($('.multi-field', $wrapper).length > 1)
                $(this).parent('.multi-field').remove();
        });
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

Wrap your code in document-ready handler

$(document).ready(function() {

});

enter image description here

Comments

0

As you are saying that your code is working on the page you showed us.

Just try this :

http://api.jquery.com/ready/


$( document ).ready(function() {
$('.multi-field-wrapper').each(function() {
            var $wrapper = $('.multi-fields', this);
            $(".add-field", $(this)).click(function(e) {
                $('.multi-field:first-child', $wrapper).clone(true).appendTo($wrapper).find('input').val('').focus();
            });
            $('.multi-field .remove-field', $wrapper).click(function() {
                if ($('.multi-field', $wrapper).length > 1)
                    $(this).parent('.multi-field').remove();
            });
        });
});

Comments

0
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="javascript/jquery-2.1.0.min.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
    $('.multi-field-wrapper').each(function() {
        var $wrapper = $('.multi-fields', this);
        $(".add-field", $(this)).click(function(e) {
            $('.multi-field:first-child', $wrapper).clone(true).appendTo($wrapper).find('input').val('').focus();
        });
        $('.multi-field .remove-field', $wrapper).click(function() {
            if ($('.multi-field', $wrapper).length > 1)
                $(this).parent('.multi-field').remove();
        });
    });
});
</script>
<title>AQUATAP - Gestor de Información - Añadir Pedido</title>
</head>

You need to kick things off with a document ready function.

"A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you".

Taken from http://learn.jquery.com/using-jquery-core/document-ready/

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.