0

I am a little baffled by this issue I encountered.

I have an <input type="text" /> element in a template that I am cloning when creating object items.

function buildSectionTest(item) {
if (item == null)
    return;
var sectionItem = $('.templates-mc .templates-mc-section-item').clone();

$('.mc-label', sectionItem).text(item.TestDescription);
$('.mc-input', sectionItem).attr({ 'testid': item.ID, 'syntax': item.TestSyntax, 'value': item.Value });
//$('.mc-input', sectionItem).val("TEST");
//alert($('.mc-input').attr('value');
//alert($('.mc-input').val());

return sectionItem.html(); 
}

However, the value property is not being set. Looking at the rendered html reveals:

<input class="mc-input rounded-corners" type="text" testid="6446" syntax="[input]">

I have tried a few things (commented out in the above code) but all return empty string, or no value.

Initially I thought that I may be clearing all input fields (for what ever strange reason), but this is not the case.

my item.Value is not null or undefined.

Has anybody else encountered this issue?

Appreciate all input!

2
  • Try logging the contents of item.Value are you sure it's not [input]
    – Orangepill
    Jun 20, 2013 at 4:41
  • Also share how buildSectionTest is used? Jun 20, 2013 at 4:43

2 Answers 2

3

don't use .attr() to set the value use .val()

$('.mc-input', sectionItem).attr({ 'testid': item.ID, 'syntax': item.TestSyntax}).val(item.Value);

Ex:

function buildSectionTest(item) {
    if (item == null)
        return;

    var sectionItem = $('.templates-mc .templates-mc-section-item').clone();

    $('.mc-label', sectionItem).text(item.TestDescription);
    $('.mc-input', sectionItem).attr({ 'testid': item.ID, 'syntax': item.TestSyntax}).val(item.Value);
    //$('.mc-input', sectionItem).val("TEST");
    //alert($('.mc-input').attr('value');
    //alert($('.mc-input').val());

    return sectionItem; //do not return the html
}
3
  • I have used .val as well, this is why I find it strange that it isn't working. If you see I have commented out a couple things in my code where I directly tested .val("test"); but it's not setting it at all.
    – Sash
    Jun 20, 2013 at 4:42
  • Also do not return sectionItem.html(); you need to return sectionItem; Jun 20, 2013 at 4:43
  • Thank you Arun - removing the .html() seems to have worked. I assume that sectionItem preserves the jquery object.
    – Sash
    Jun 20, 2013 at 4:50
-2

Try this:

$('.mc-input', sectionItem).attr({ 'testid': item.ID, 'syntax': item.TestSyntax, this.val(item.Value) });
2
  • 1
    It's not even valid Javascript syntax. Everything inside an object literal has to be in the form property: value.
    – Barmar
    Jun 20, 2013 at 4:46
  • And you're not using this properly at all.
    – Barmar
    Jun 20, 2013 at 4:47

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.