0

How do you get the value of an html element attr for an ajax call?

my code:

$('#manager_pass').submit(function(evt){ 

    evt.preventDefault();

        $.ajax({

            url: App.managerpassURL,
            data: {
                ????????????
            },
            dataType: 'json',
            type: 'POST',
            success: function(r){
                box_2.animate({'top':'-250px'},500,function(){
                overlay.fadeOut('fast');
                    $('.action_wrapper').fadeOut(1000, function(){
                            $('.form').fadeIn(1000);
                    });
                });
            }


        });

});

I need to get the value from here

<input type="text" name="managerPassword" value="" class="man_code">

Thanks

Aessandro
  • 5,517
  • 21
  • 66
  • 139

3 Answers3

1

Something like this if you want to send JSON:

JSON.stringify({ "pass" : $('input[name="managerPassword"]').val() })

Of course make sure its secured if you want to send a password!

ramsinb
  • 1,985
  • 12
  • 17
  • 1
    data: { JSON.stringify({ "pass" : $('input[name="managerPassword"]').val(); }) }, – Aessandro Sep 26 '12 at 11:57
  • You should try `data : JSON.stringify({ "pass" : $('input[name="managerPassword"]').val() })` – ramsinb Sep 26 '12 at 12:06
  • not working :( data : JSON.stringify({ "pass" : $('input[name="managerPassword"]').val() }), – Aessandro Sep 26 '12 at 12:10
  • What's the error? can you try printing to the console `console.log(JSON.stringify({ "pass" : $('input[name="managerPassword"]').val() }))` just before you are calling `$.ajax` and tell us what you see? – ramsinb Sep 26 '12 at 12:12
  • success: function(r){ box_2.animate({'top':'-250px'},500,function(){ overlay.fadeOut('fast'); $('.action_wrapper').fadeOut(1000, function(){ $('.form').fadeIn(1000); }); }); } – Aessandro Sep 26 '12 at 12:16
  • {"pass":"dddsa3321"} 77b7abdc35:654 XHR finished loading: "http://projectURL". jquery.min.js:2 send jquery.min.js:2 p.extend.ajax jquery.min.js:2 (anonymous function) 77b7abdc35:656 p.event.dispatch jquery.min.js:2 g.handle.h – Aessandro Sep 26 '12 at 12:19
  • Looks like the AJAX part is working then, you'll need to look into why the stuff in the success callback are failing. I'm not sure what box_2 or overlay are in this context... – ramsinb Sep 26 '12 at 12:22
  • hi overlay and box2 simply replace id values inside html nothing more – Aessandro Sep 26 '12 at 12:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17186/discussion-between-ramsinb-and-alex) – ramsinb Sep 26 '12 at 12:36
0

If you specify an id attribute on your input element, you can do the following to get the value in js:

document.getElementById('exampleid').value
Mitch Satchwell
  • 4,770
  • 2
  • 24
  • 31
0

$('#manager_pass').submit(function(evt){

evt.preventDefault();
var passtxt=$('input:text[name=managerPassword]').val();
    $.ajax({

        url: App.managerpassURL,
        data: {
            passtxt:passtxt
        },
        dataType: 'json',
        type: 'POST',
        success: function(r){
            box_2.animate({'top':'-250px'},500,function(){
            overlay.fadeOut('fast');
                $('.action_wrapper').fadeOut(1000, function(){
                        $('.form').fadeIn(1000);
                });
            });
        }


    });

});

Jhanvi
  • 594
  • 3
  • 17
  • http://stackoverflow.com/questions/503627/how-to-pass-multiple-parameters-in-json-format-to-a-web-service-using-jquery try in this way it may help you if JSON.stringify does not work – Jhanvi Sep 27 '12 at 09:24