0

I have 2 fields on a form that I need to be filled with the same data.

when you type here:

<?php $value = (isset($lead) ? $lead->name : ''); ?>
        <?php echo render_input('name','lead_add_edit_name',$value); ?>

this is us updated with the same info:

<?php $value = (isset($lead) ? $lead->company : ''); ?>
        <?php echo render_input('company','lead_company',$value); ?>

I found this code but, I can't get it to work:

    $("#thing1").bind('input', function () { var stt = $(this).val(); $("#thing2").val(stt); });

Any Ideas? Thanks

Lorenzo B
  • 27
  • 4
  • 1
    Try to encapsulate it in `$(document).ready(function(){})`; – Rahul Feb 20 '19 at 12:55
  • 2
    btw. `bind` is depricated, use `on` – Pinke Helga Feb 20 '19 at 12:59
  • "can't get it to work" is very vague. Does the event handler fire? do you have two inputs with id `thing1` and `thing2` - looks like this should be: `$("#lead_add_edit_name").on('input', function () { var stt = $(this).val(); $("#lead_company").val(stt); });` or `$("#name").on('input', function () { var stt = $(this).val(); $("#company").val(stt); });` - can you provide the *HTML* - not the useless php wrapper as it could be creating anything... – freedomn-m Feb 20 '19 at 13:07
  • Do you get any errors in your browser console (eg `$ not defined`). What happens if you add `console.log($("#thing1").length)` directly above your `bind` code? If it's zero, then the selector is wrong. Is your code running before the HTML has rendered? If so, add inside a doc ready (as per first comment) – freedomn-m Feb 20 '19 at 13:08

1 Answers1

1

you would need to bind the right DOM event on the form field.

First of all, in this case, identify the two field (origin and destination) you would like to copy data over. In your case, you seem to be using a sort of PHP framework (a wordpress theme?), that use the render_input function to render a form input field.

Supposing the first parameter of that function represents the name of the field, you would select the fields by following selectors:

$("input[name=name]")
$("input[name=company]")

by knowing this, you now will need to intercept the DOM event on the first field that occours when you change the value, so the correct event would be 'change'. And as @Rahul said, the correct listener would be 'on', no more 'bind'.

$("input[name=name]").on('change',function(){...});

Lastly, when the event triggers, you would just have to copy the value of the element to the other one.

$("input[name=name]").on('change',function(){
    var fieldValue = $(this).val();
    $("input[name=company]").val(fieldValue);
});

Real finally, as @Quasimodo's clone said, encapsulate all the code inside a document ready triggered function so you'll be sure that it'll be executed after the form fields will be actually avalaible in page.

$(document).ready(function(){
    $("input[name=name]").on('change',function(){
        var fieldValue = $(this).val();
        $("input[name=company]").val(fieldValue);
    });
});
Jackie Degl'Innocenti
  • 1,251
  • 1
  • 14
  • 34
  • 1
    The *correct* event is actually `input` which OP had (upvoted anyway :) ) – freedomn-m Feb 20 '19 at 13:09
  • Thanks @freedomn-m, this is not the case, but if the form field would be changed by code (not UI), it would be better to listen to a 'change' event, as this would also intercept changes that are not made by the user. – Jackie Degl'Innocenti Feb 20 '19 at 13:13
  • 1
    Worth reviewing input event: https://developer.mozilla.org/en-US/docs/Web/Events/input *"Unlike `input`, the `change` event is not necessarily fired for each alteration to an element's value."* - input nicely handles changes made by code which change does not. It also combines keypress/up/down / change / cut-paste / right click / etc which change does not as change will generally only fire on blur for a text input (as per question "when you type here", not "after you type here and exit the input). – freedomn-m Feb 20 '19 at 13:19
  • Does that MDN vanilla JS doc worth as a better source than jQuery doc? https://api.jquery.com/category/events/form-events/ – Jackie Degl'Innocenti Feb 20 '19 at 14:18
  • 1
    jquery docs: "`.change()` method is just a shorthand for `.on( "change", handler )`". jquery docs form-events page (your link) "bind an event handler to the javascript event" - so, yes, documentation on the vanilla js event is better than the jquery documentation on the event which just says "this binds the [vanilla] event to a handler". Jquery also does not have a `.input()` shorthand as jquery stopped creating shorthand methods in favour of `.on` before the `input` event existed. – freedomn-m Feb 20 '19 at 15:21
  • Thanks! once my rep goes up, I'll be back to upvote. – Lorenzo B Feb 20 '19 at 16:00
  • Thanks @LorenzoB, thanks freedomn-m: upvoted. – Jackie Degl'Innocenti Feb 20 '19 at 17:01