Ullas Prabhakar
Ullas Prabhakar

Reputation: 416

Get id from serializeArray

I have the following form

 <form action="test.php" id="loginform" name="loginform" method="post">
     <input name="title[]" id="title1" type="text" value="" tabindex="1" />
     <input name="title[]" id="title2" type="text" value="" tabindex="2" />
     <input  name="title[]" id="title3" type="text" value="" tabindex="3" />
     <input type="submit" name="submit" value="Submit" id="submit" tabindex="4" />
 </form>

I am able to get the element name but not its id using this code.

$('#loginform').bind('submit', function() { 
    var elements = $(this).serializeArray();
    $.each(elements, function(i, element) {
        var temp = $('#' + element['name']);
        var name = this.name; alert(name);
        var id = this.id; alert(id); ///even id = this.attr("id"); not getting

        var value = this.value; 
        (temp.val() == '') ? temp.css({'background': '#FFC4C4', 'border': '1px solid #F00'}) : temp.removeClass('hightlight');
    });
    return false;
});

Demo

Upvotes: 0

Views: 2536

Answers (1)

Mitch Malone
Mitch Malone

Reputation: 892

Updating the answer on this for you:

This code:

var id = $(this).id;alert(id);

Should become this:

var id = $('input[name="' + name + '"]').attr("id");
alert(id);

See the code working here: http://jsfiddle.net/Ct8zf/5/

Please note that accoring to .serializeArray() documentation that the only elements which are serialized are the names and values.

Hope this helps.

Upvotes: 1

Related Questions