2

I am trying to autopopulate an input field (location-name) with the value entered in another field (location-address). I have looked online everywhere and managed to make the JS snippet work on a straightforward example, but for some reason this is not working with the following php code.

Just to clarify, I would like the value entered in "location-address to be passed to "location-name".

<script>
    $('#location-address')
        .keyup(function() {
            var value = $(this).val();
            $('#location-name').text(value);
        })
        .keyup();
</script>

<tr class="em-location-data-name">
    <th style="padding-top: 14px;">
        <?php _e ( 'Event unique identifier:', 'dbem' )?>
    </th>
    <td>
        <input id="location-name" type="text" name="location_name" />
    </td>

</tr>
<tr class="em-location-data-address">
    <th style="padding-top: 14px;">
        <?php _e ( 'Address:', 'dbem' )?>&nbsp;</th>
    <td>
        <input id="location-address" type="text" name="location_address" value="<?php echo esc_attr($EM_Location->location_address, ENT_QUOTES); ; ?>" />
    </td>
</tr>
2
  • Have you wrapped your code in document-ready handler? Commented Apr 15, 2016 at 13:06
  • Thanks Satpal. I just added it, but it still doesn't work ... $(document).ready(function() { $( '#location-address' ) .keyup(function() { var value = $( this ).val(); $( '#location-name' ).text( value ); }) .keyup(); }); Commented Apr 15, 2016 at 13:11

3 Answers 3

2

Firstly your code needs to be placed in a document.ready handler. Secondly, you need to use val() not text() to set the value of an input field.

$(function() {
    $('#location-address').keyup(function() {
        var value = $(this).val();
        $('#location-name').val(value);
    }).keyup();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr class="em-location-data-name">
    <th style="padding-top: 14px;">
      <?php _e ( 'Event unique identifier:', 'dbem' )?>
    </th>
    <td>
      <input id="location-name" type="text" name="location_name" />
    </td>
  </tr>

  <tr class="em-location-data-address">
    <th style="padding-top: 14px;">
      <? php _e( 'Address:', 'dbem') ?>&nbsp;
    </th>
    <td>
      <input id="location-address" type="text" name="location_address" value="<?php echo esc_attr($EM_Location->location_address, ENT_QUOTES); ; ?>" />
    </td>
  </tr>
</table>

Sign up to request clarification or add additional context in comments.

2 Comments

this worked ... Thank you so much Rory !!! i am a newbie ... i spent hours on this ... and couldn't figure it out ... thanks so much!
No problem, glad to help
0

You need to wrap your jQuery code in $(document).ready(); so that jQuery binds all HTML elements.

Corrected code:

<script>
$(document).ready(function(){
$('#location-address').keyup(function() {
    var value = $(this).val();
    $('#location-name').val( value );
  });
}).keyup();
</script>

1 Comment

@RoryMcCrossan, thanks for the comment. I have updated my answer.
0

Use this

<script>
$(document).ready(function(){
$('#location-address').keyup(function() {
    var value = $(this).val();
    $('#location-name').val( value );
  });
});
</script>

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.