0

I have a button that when pressed opens a new window, in that window the user chooses a file and clicks it. When clicked, the value should pass to a hidden input in the form and closes the window. Everything acts like it should be, the only thing that its not working, is the fact that the input value stays empty.

This is my input:

<input name="value_pdf" value="" type="hidden" id="value_pdf" />

And here is the function:

$(".insert").click(
    function () {
            var file = $(this).attr("rel"); //its correct and passes the value I want
            var value = "value_<?php echo $_GET['but'];?>" //again, this is correct too, I've used console.log() to check

            opener.$("#value_pdf").val(file);
            //I've done a console.log() here to to check if #value_pdf had a value and it was correct, it logged something like "database.pdf"

            window.close();
            return false;
    }
);

My question is, where did I do it wrong? I seems that everything is right...

EDIT: Turns out, somewhere in my code I had this same ID on a DIV. Althought I advise you to check the choosen answer if you have this same problem, it made my code better.

rafaelmorais
  • 1,323
  • 2
  • 24
  • 49

2 Answers2

1

I've never tried opener.$(...) but it looks like it should work, providing the opener window still exists and there's no cross-domain issue.

If it doesn't work then you might try writing a function in the opener and calling it from the child window.

In the opener (in the global namespace or in some namespace accessible from global) :

function setValue(selector, value) {
    $(selector).val(value);
}

In the child window :

window.opener.setValue("#value_pdf", file);

I can't really see why this would work when opener.$(...) didn't because there's no fundamental difference. But hey, you never know ...

Roamer-1888
  • 19,138
  • 5
  • 33
  • 44
  • I made like you send, and before $(selector).val(value); I had it a console.log just to check. And it worked, but didn't change the input value. – rafaelmorais Dec 16 '14 at 14:35
  • Then either `$` (jQuery) is failing, or the selector is pointing to an element that doesn't (yet) exist. – Roamer-1888 Dec 16 '14 at 14:39
  • jQuery is working, I've tried with other selector and worked. Maybe I should have mentioned that this was created on a input that was already cloned. I have a button to duplicate inputs so the user could add more that one file, and in the first input it works greats. But not on the second, third, forth... Is it possible that DOM wasn't loaded? – rafaelmorais Dec 16 '14 at 14:42
  • If you clone an element then you can't rely on the original id. You need to change the clone's id or find some other way to address the clone. – Roamer-1888 Dec 16 '14 at 14:45
  • 1
    Yes, I was "IDing" as value_1, value_2. That was right, but I found what my problem was. I had this ID duplicated in some other place. But this answer was truly helpful and I will select it. – rafaelmorais Dec 16 '14 at 14:47
  • 1
    Cool! Good luck with it. – Roamer-1888 Dec 16 '14 at 14:48
0

If you have php enabled I would use that instead of sending via javascript. And if you need the information on the same page, i would use AJAX.

http://api.jquery.com/jquery.post/

a sample would look like this.

$.post( "test.php", $( "#testform" ).serialize() );
<form id="testform">
<input name="something">
</form>

This would take a form and submit the input named "something" and post it to the "test.php" page.

Roger
  • 3,226
  • 1
  • 22
  • 38
  • 1
    OP is working with two windows, a parent (opener) and its child. He wants to send something from the child to the parent (without reloading either page). – Roamer-1888 Dec 16 '14 at 14:11