3 Replies - 4355 Views - Last Post: 21 June 2013 - 04:09 AM

#1 NoRemorse  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 11

JQuery returns undefined as value.

Posted 21 June 2013 - 02:31 AM

<form action="/settings" method="POST">
   <ul style="list-style-type:none;margin:0;padding:0;">
        <li><input type="checkbox" class="checkbox" value="slashdot.org -- http://rss.slashdot.org/Slashdot/slashdot" checked=""><b style="padding-left:15px;">slashdot.org -- http://rss.slashdot.org/Slashdot/slashdot</b></li>
        <li><input type="checkbox" class="checkbox" value="postimees.ee -- http://www.postimees.ee/rss/" checked=""><b style="padding-left:15px;">postimees.ee -- http://www.postimees.ee/rss/</b></li>
   </ul>
   <div>
       <button class="btn submit" id="submit">Submit</button>
   </div>
   <input type="hidden" name="sources" id="sources" class="sources" value="">                
</form>
<script>
    var feedValue="";
$('form').submit(function(){
    $(".checkbox").each(function(){
        if($('this').prop('checked',true)){
            feedValue = feedValue + '+' + $('this').val();
            console.log($('this').val());
        };
    });
    $('#sources').attr('value',feedValue);
    alert("done");
});

</script>


Even though checkboxes have values, when I go over them and check which ones are checked and write them to a string, I get undefined for their values. The exact output , which is not showed in this code is: +undefined+undefined . It also still occurs when I uncheck the checkboxes. Does anyone have any idea what is wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: JQuery returns undefined as value.

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,179
  • Joined: 08-June 10

Re: JQuery returns undefined as value.

Posted 21 June 2013 - 02:41 AM

there are a couple of things wrong.
  • what is $('this') supposed to be? it is neither a selector nor an htmlString. essentially, jQuery would create an empty jQuery object which of course does not have a value.
  • $().prop(attribute, value) is a setter, not a comparison. jQuery setters usually return a jQuery object, which is a truthy value (treated as true in a comparison like if()), so your condition always triggers.
  • there is no need to set the value attribute of the #sources field, jQuery has a method for that: .val()

Was This Post Helpful? 1
  • +
  • -

#3 NoRemorse  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 11

Re: JQuery returns undefined as value.

Posted 21 June 2013 - 02:58 AM

Well that was supposed to mean that, it is the item which is being gone over by the loop. (thank you very much a friend who told me that). How could I check the item that is the current one in the loop? I also changed the prop()
code right now is:
    var feedValue="";
    $('form').submit(function(){
        $(".checkbox").each(function(){
            if($(this).is(':checked')){
                feedValue = feedValue + '+' + $(this).val();
                console.log($(this).val());
            };
        });
        $('#sources').val(feedValue);
        alert("done");
    });


Changed: 'this' to : this.

The result now is as expected.

Thank you very much for your help!

This post has been edited by NoRemorse: 21 June 2013 - 03:10 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,179
  • Joined: 08-June 10

Re: JQuery returns undefined as value.

Posted 21 June 2013 - 04:09 AM

you could even easily omit the if() condition
$('form').submit(function(){
    var feedValue="";
    $(".checkbox:checked").each(function(){
        feedValue += '+' + $(this).val();
        // or even
    //  feedValue += '+' + this.value;
    });
    $('#sources').val(feedValue);
});

This post has been edited by Dormilich: 21 June 2013 - 04:11 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1