/    Sign up×
Community /Pin to ProfileBookmark

writting to hidden values and order of events.

I have a web page that calls a function during the onload event, it changes the value of a hidden input variable. I am using a document.write to write the hidden variable to the page. This isn’t working, i can see the new value in my DOM inspector and call it in an alert, is it because of some restrictions with document.write?

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@geolevDec 08.2006 — Are you trying to update a hidden form field or a "variable" as you say?

George
Copy linkTweet thisAlerts:
@spangltkauthorDec 08.2006 — hidden form field, sorry, thanks for the clarification
Copy linkTweet thisAlerts:
@coppocksDec 08.2006 — As long as the document.write comes after the variable is set, it should work.

var test = 'testValue';

document.write('the value of test is ' + test);

// OR

document.write('<input type="hidden" name="test" value="' + test + '">');
Copy linkTweet thisAlerts:
@spangltkauthorDec 08.2006 — var test =document.frm1.isbn_out.value ;

document.write(test+'...');

only writes ... to the page.

if i enter " Javascript: alert(document.frm1.isbn_out.value); " in the URL it gives me the new value for the field. I can see the new value in the DOM inspector as well.
Copy linkTweet thisAlerts:
@coppocksDec 08.2006 — I tried this and it worked fine...
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script&gt;
function testVal() {
var test2 = document.frm1.isbn_out.value;
//var test2 = 'TestMe';
return test2;
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form name="frm1"&gt;
&lt;input type="hidden" name="isbn_out" value="Whatever"&gt;
&lt;script&gt;
document.write(testVal() + '...');
&lt;/script&gt;


Post some code so I can take a look-see what you're attempting.
Copy linkTweet thisAlerts:
@spangltkauthorDec 08.2006 — well theres a lot of code, can i sum it up?

the purpose of this is to convert a 10 digit ISBN # into a 13 digit ISBN #

2 form fields:

document.frm1.isbn_in.value='5428943546' // 10-digit isbn value

document.frm1.isbn_out.value='' // 13-digit isbn value

the function is called in the body tag: <body onLoad="cnvrtISBN()">


the function set the isbn_out field value when its done:

frm1.isbn_out.value = isbnnum+z;

then write it with document.write(document.frm1.isbn_out.value);
Copy linkTweet thisAlerts:
@coppocksDec 08.2006 — You want the function cnvrtISBN() to place the value in a hidden form field?

If so, I am also assuming that cnvrtISBN() generates the data during onLoad, so... you could create a var for the input value in cnvrtISBN(), for example:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script&gt;
var test;
function cnvrtISBN() {
var test1 = document.frm1.isbn_in.value;
document.frm1.isbn_out.value = test1 + '123'; /* whatever you do to get 13 digits, this is only an example */
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body onLoad="cnvrtISBN();"&gt;
&lt;form name="frm1"&gt;
&lt;input type="hidden" name="isbn_in" value="5428943546"&gt;
&lt;input type="text" name="isbn_out"&gt;


I used an open text field in that example so you could see the value being passed from the cnvrtISBN() function. Just make it hidden if that's what's needed.

This what you mean?
Copy linkTweet thisAlerts:
@spangltkauthorDec 08.2006 — yeah, and from the DOM inspector, and alert()ing the value, i can see that the function works correctly and that the value is there. however document.write() isn't writing the value. It will write the value for isbn_in but not for isbn_out. is this becuase document.write is only writing what is in the html hard code? (isbn_out is blank, so maybe it's writing that ) or is there another reason it wouldn't be writing it?
Copy linkTweet thisAlerts:
@coppocksDec 08.2006 — I don't know. Since I can't see all the code, I am not sure. Why document.write it when you can insert it dynamically as I showed previously? But if you must document.write the field, then you are going to have to return a value from the function.

document.write('<input type="hidden" name="isbn_out" value="' + cnvrtISBN() + '">';

where cnvrtISBN() does something like this:

function cnvrtISBN() {

var oldValue = "2311232132";

// ... do your 13 digit stuff here

var newValue = "2311232132139"; // example

return newValue;

}

then 2311232132139 will be available from the hidden field.
×

Success!

Help @spangltk 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.20,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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