tags:

views:

153

answers:

3

Hi, I'm trying to get the text of a span tag and add it to a hidden field, any ideas how this is possible with jquery.

thanks

 <form>  
  <span class="descriptionsPizza">EXTRA CHEESE</span>
  <input name="textfield1" type="text" id="textfield1" class="valfield" size="2" maxlength="2" value="0" />

  <span class="descriptionsPizza">HAM</span>
  <input name="textfield1" type="text" id="textfield1" class="valfield" size="2" maxlength="2" value="0" />

 </form>

so i'd like to add the EXTRA CHEESE to a hidden field.

+1  A: 

Push the text items into an array then join the array to add it into the hidden input. The demo below enters the text with a space in between each item as you do not specify why/how you want it to appear inside. There are however better approaches. Have you considered non js clients?

    var spanText=[];

    $('span.descriptionsPizza').each( function(){

        var $span = $(this);
        //is value of the next input greater than 0?
        if ( $span.next().val() > 0 ){
           spanText.push( $(this).text() );
        }

    });


    $('input[name=qty]').val( spanText.join(' ') );
redsquare
sorry I forgot to mention I have more than one .descriptionsPizzahow would I be able to do that with more fields.
amir
Hi thanks for replying, is it possible to get the text only if the quantity of that item is greater than 0.thanks
amir
Do you want to pay me to complete your project:)
redsquare
mark the answer and add another question....rather than changing the goalposts every 5 mins
redsquare
sorry, I've been stuck with this for some time, thanks for your help anyway.
amir
How is the quantity being set in the first place?
T B
by the user I assume with + - buttons iirc from his earlier questions
redsquare
He changed the context of his question. I thought he just wanted all the spans in one hidden input.....
redsquare
+1  A: 
$("input[name=qty]").val($("span.descriptionPizza"));
cletus
+1  A: 
$("span.descriptionsPizza").each(function(){
    $(this).next("input").val($(this).text());
});

This is assuming the hidden input is directly after the span.descriptionsPizza

T B