/    Sign up×
Community /Pin to ProfileBookmark

Please help me extend this parent-child passback script

I’m using javascript to allow a user to select a colour-swatch for a product.

On one “main” page, a “select colour” link opens a new popup window, which has the colour swatch images on it. When the user clicks on one of these images, the popup window closes, and a text label associated with the image is passed back to a form field in the original window. This code, using the alt tags to pass the names, works fine:

main page (parent.html):

In <head>:

<script type=”text/javascript”>
var objActiveElem;
var winPop;
function GetImage(xElem){
objActiveElem = xElem;
winPop = window.open(“imagePicker.html”);
winPop.focus();
}
</script>

… and in the <body> a form:

<form name=”Form1″>
<input type=”button” name=”btn1″ value=”Choose Colour” onclick=”GetImage(document.Form1.txt1)”>
<br>
<input type=”text” name=”txt1″ value=”(none selected)” readonly>
<br>
</form>

popup/child window (imagePicker.html):

In <head>:

<script type=”text/javascript”>
function SetImage(xValue){
window.opener.objActiveElem.value = xValue;
window.opener.objActiveElem = “”;
self.close();
}
window.onunload = function(){
window.opener.objActiveElem = “”;
}
</script>

… and in the <body>:

<img id=”img1″ src=”images/colour1.jpg” alt=”RED” onclick=”SetImage(this.alt)”><br>
<img id=”img2″ src=”images/colour2.jpg” alt=”GREEN” onclick=”SetImage(this.alt)”><br>
<img id=”img3″ src=”images/colour3.jpg” alt=”BLUE” onclick=”SetImage(this.alt)”><br>
<table width=”100″ border=”1″ cellspacing=”2″ cellpadding=”0″ height=”150″>
<tr><td>image appears here</td></tr>
</table>

… But! I’d like the source path of the image also to be passed, so that the selected image is also displayed in the main page. That is, both image “id” (from alt tag, displaying in form field) and image URL (from?, displaying in table). This is probably very simple stuff, but I don’t know how to do it! Can anyone help?

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@KorJul 22.2005 — 
But! I'd like the source path of the image also to be passed
[/quote]


SetImage(this.alt,[B]this.src[/B])

but be aware... the returned value is the absolute url, not the relative, so that, if you still need the relative url, you must later split and reconstruct the passed parameter
Copy linkTweet thisAlerts:
@nevauthorJul 22.2005 — Yes I knew it would be SetImage(this.alt,this.src) but I'm unsure about the rest of the coding - how to return the image source and display it in a table. Could you insert it into my code to clarify? Thanks in advance!!!
Copy linkTweet thisAlerts:
@KorJul 22.2005 — Where exatly you want to insert that picture?
Copy linkTweet thisAlerts:
@nevauthorJul 22.2005 — I was going to place it in a table of its own, to the right of the "txt1" text field.
Copy linkTweet thisAlerts:
@KorJul 22.2005 — ... re-edit... I have tested the code and it is not working as it should...
Copy linkTweet thisAlerts:
@nevauthorJul 22.2005 — That's fantastic - thanks! Except - clicking again on the "btn1"/"Choose Colour" button results in pictures being added -- is it possible to tweak it so that images selected from further clicks replace the old image (so only one image ever appearing in the table?)?

Thanks very much for sharing your knowledge and expertise with me! ?
Copy linkTweet thisAlerts:
@KorJul 22.2005 — ok i got it

[code=php]
<script type="text/javascript">
function SetImage(xValue,url){
window.opener.objActiveElem.value = xValue;
window.opener.objActiveElem = "";
var oPic = opener.document.createElement('img');
oPic.setAttribute('src',url);
var root = opener.document.getElementById('mycell');
root.replaceChild(oPic,root.firstChild)
self.close();
}
window.onunload = function(){
window.opener.objActiveElem = "";
}
</script>
[/code]


Anf you cell must have a nonbreaking space (correction for IE)

<td id="mycell">&nbsp;</td>
Copy linkTweet thisAlerts:
@nevauthorJul 22.2005 — It works beautifully! That's very much appreciated - thank you again!!!!
×

Success!

Help @nev spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...