0

I am trying to get the value of an input which is in an iframe.

When I submit the form I'm getting the alert telling me it's been submitted but when I try to get the value of the input it's alerting Object instead of the value.

Here is the code:

$(document).ready(function() {
    $('#myiframe').load(function() {
        $(this).contents().find('#myiframe').submit(function() {

            $res = $("#myiframe").contents().find('input[name="subject"]');

            alert('Form HAS been submitted'); //This works

            alert($res); // returns Object Object

            return true;
        });
    });

});

What I'm I doing wrong here?

4
  • 3
    tried alert($res.val()); ? Commented Aug 26, 2014 at 9:40
  • Yes, that won't alert for some reason Commented Aug 26, 2014 at 9:42
  • 1
    can you show us the html in iframe? Commented Aug 26, 2014 at 9:43
  • 2
    what about alert($($res).val()) ? Commented Aug 26, 2014 at 9:44

2 Answers 2

2

It is because you are getting an object back. Try querying for the value instead.

// ..
    alert($res.val())
// ..
Sign up to request clarification or add additional context in comments.

3 Comments

what is the need of $() again?
@AnoopJoshi A '$' in a variable means nothing to the interpreter, much like an underscore. So if jQuery is not bound to the fetched element one would still be able to call jquery funcntions such as val() if enquoted with $()
Thats not my question. In his code $res is already a jquery object. You dont need to make it again as jquery object by wrapping up with $()
0

$res is a jquery object. You need to get the value using .val() inorder to alert the value.

$(document).ready(function() {
    $('#myiframe').load(function() {
        $(this).contents().find('#myiframe').submit(function() {

            $res = $("#myiframe").contents().find('input[name="subject"]');

            alert('Form HAS been submitted'); //This works

            alert($res.val()); // returns Object Object

            return true;
        });
    });

});

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.