2

I have php foreach loop, where .data_fromCity contains different value for each element. Each item has form with input. What i want to do is to get EACH $fromCity variable value and append it to that input.

<?php foreach( $rows as $row ) : { ?>

<div class="data_fromCity"><?php echo $fromCity; ?></div>
<form><input type="text" name="cityinput" value=""></form>

<?php } ?>

I was thinking about adding to that loop something like that, but it adds text of all php items in one line for all items.

<script> $("input[name='cityinput']").val($('.data_fromCity').text()); </script>

It should be easy in jquery, but i'm not good at jquery :) Thanks for any help!

The source code what is rendered on frontend is:

<div class='wrapper'>

    <div class="data_fromCity">London</div>
    <form><input type="text" name="cityinput" value=""></form>

    <div class="data_fromCity">Rome</div>
    <form><input type="text" name="cityinput" value=""></form>

    <div class="data_fromCity">Paris</div>
    <form><input type="text" name="cityinput" value=""></form>

    <div class="data_fromCity">Madrid</div>
    <form><input type="text" name="cityinput" value=""></form>

</div>
6
  • You can't do <input type="text" name="cityinput" value="<?php echo $fromCity; ?>"> ? Commented Apr 14, 2015 at 13:35
  • @Drakes why? it is php loop and i'm getting that variable for each item. Commented Apr 14, 2015 at 13:36
  • Where does $fromCity come from? In your loop it looks like it will always stay the same. Commented Apr 14, 2015 at 13:39
  • @Drakes i'm getting it from custom field ($fromCity= $row['from_city'];) Forgot to add it to code. So it's different for each item. Commented Apr 14, 2015 at 13:42
  • Then just do it all in PHP like in my previous comment. Why even use jQuery? What am I missing? You want to copy the text in <div class="data_fromCity"> to the value of the <input> just below it, right? Commented Apr 14, 2015 at 13:43

2 Answers 2

2

You don't need a wrapper. You can do it in 3 lines.

$inputs = $('input[name="cityinput"]');
$('.data_fromCity').each(function(i, obj) {
    $($inputs.get(i)).val( $(obj).text() );
});

Demo: http://jsfiddle.net/ya3vewgo/

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

3 Comments

You selected an answer just 50 seconds before I could post this, but try this out just for fun so you don't have to add any more HTML
yeah that works perfectly too :) Thank you very much. You guys are awesome. So much help and so fast problem solving here.
Upvote when you get two more rep then k ... and now you can. ;)
1

With wrapper it will be easier:

<?php foreach( $rows as $row ) : { ?>

    <div class='wrapper'>
      <div class="data_fromCity"><?php echo $fromCity; ?></div>
      <form><input type="text" name="cityinput" value=""></form>
    </div>

<?php } ?>


<script type='text/javascript'>

    $.each($('.wrapper'), function(){
        $this = $(this);
        text = $this.find('.data_fromCity').text();
        $this.find('input[name="cityinput"]').val(text);
    });

</script>

10 Comments

No, it doesn't work :( it is also getting all data_fromcity values as one big word in each item input.
show me pls, what you get in <div class="data_fromCity"><?php echo $fromCity; ?></div> how looks $fromCity ?
$fromCity = is f.e. London. So i'm getting f.e. 4 items where i have fromcity variable = some city and form input for each of item. So 4 divs with city, 4 inputs where that city should append to.
pls, just add to your answer your part with it from results html page.
just edited my first comment in this question see part "The source code what is rendered on frontend is"
|

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.