Monty
Monty

Reputation: 1322

Javascript & JQuery odd issue or what did I do wrong?

I have this function for coping values from window to window that is working one way, but not another...

Working script:

$(document).ready(function(e) {
    $('#clickit').live({
        click: function() { 
            window.opener.document.forms['orderForm']['service'].value = document.forms['GroundRates']['service'].value;
            window.opener.document.forms['orderForm']['rate'].value = document.forms['GroundRates']['rate'].value;
            self.close();
            return false;
        }
    });
});

Now I on this other script, what did I do wrong? I'm pulling my hair out here.

Not working:

$(document).ready(function(e) {
    $('#clickit').live({
        click: function() { 
            var thisservice = document.forms['GroundRates']['service'].value;
            var thisrate = document.forms['GroundRates']['rate'].value;
            var thatservice = window.opener.document.forms['orderForm']['service'].value;
            var thatrate = window.opener.document.forms['orderForm']['rate'].value;
            $(thatrate) = $(thisrate);
            $(thatservice) = $(thisservice);
            self.close();
            return false;
        }
    });
});

I've also tried..

$(thatrate).val() = $(thisrate).val();
$(thatservice).val() = $(thisservice).val();

And..

thatrate = thisrate;
thatservice = thisservice;

But this works:

var service = document.forms['GroundRates']['service'].value;
var rate = document.forms['GroundRates']['rate'].value;
window.opener.document.forms['orderForm']['service'].value = service;
window.opener.document.forms['orderForm']['rate'].value = rate;

Am I not assigning the var correctly for the window.opener?

Upvotes: -1

Views: 115

Answers (4)

aziz punjani
aziz punjani

Reputation: 25776

var thisservice = document.forms['GroundRates']['service'];
var thisrate = document.forms['GroundRates']['rate'];
var thatservice = window.opener.document.forms['orderForm']['service'];
var thatrate = window.opener.document.forms['orderForm']['rate'];

thatrate.value = thatservice.value;
thatservice.value = thisservice.value;

or if you want to wrap the DOM objects with a jQuery object.

var thisservice = document.forms['GroundRates']['service'];
var thisrate = document.forms['GroundRates']['rate'];
var thatservice = window.opener.document.forms['orderForm']['service'];
var thatrate = window.opener.document.forms['orderForm']['rate'];

$(thatrate).val( $(thatservice).val() );
$(thatservice).val( $(thisservice).val() );

Upvotes: 1

RightSaidFred
RightSaidFred

Reputation: 11327

Your console will tell you: ReferenceError: Invalid left-hand side in assignment

$(thatrate) = $(thisrate);
$(thatservice) = $(thisservice);

You should do it like this:

var thisservice = document.forms['GroundRates']['service'];
var thisrate = document.forms['GroundRates']['rate'];
var thatservice = window.opener.document.forms['orderForm']['service'];
var thatrate = window.opener.document.forms['orderForm']['rate'];

thatrate.value = thisrate.value
thatservice.value = thisservice.value;

Upvotes: 1

DrXCheng
DrXCheng

Reputation: 4132

try:

var thisservice = document.forms['GroundRates']['service'];
var thisrate = document.forms['GroundRates']['rate'];
var thatservice = window.opener.document.forms['orderForm']['service'];
var thatrate = window.opener.document.forms['orderForm']['rate'];
thatrate.val(thisrate.val());
thatservice.val(thisservice.val());

Upvotes: 1

Toast
Toast

Reputation: 424

You're misusing .val()

$(thatrate).val($(thisrate).val());
$(thatservice).val($(thisservice).val());

The new value goes inside the parentheses.

Upvotes: 2

Related Questions