1

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
  • Are you sure this is what you want to do? How is the text getting into the span in the first place? Your example shows it as hardcoded, so why not just hardcode it into the hidden filed? if dynamic then you need to add it to the onClick or Change event.
    – Tony Borf
    Aug 3, 2009 at 13:59

3 Answers 3

1

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(' ') );
8
  • sorry I forgot to mention I have more than one .descriptionsPizza how would I be able to do that with more fields.
    – akano1
    Aug 3, 2009 at 13:54
  • Hi thanks for replying, is it possible to get the text only if the quantity of that item is greater than 0. thanks
    – akano1
    Aug 3, 2009 at 13:58
  • Do you want to pay me to complete your project:)
    – redsquare
    Aug 3, 2009 at 13:59
  • mark the answer and add another question....rather than changing the goalposts every 5 mins
    – redsquare
    Aug 3, 2009 at 13:59
  • sorry, I've been stuck with this for some time, thanks for your help anyway.
    – akano1
    Aug 3, 2009 at 14:00
1
$("input[name=qty]").val($("span.descriptionPizza"));
1
$("span.descriptionsPizza").each(function(){
    $(this).next("input").val($(this).text());
});

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.