/    Sign up×
Community /Pin to ProfileBookmark

i’m trying to set a varialbe to text that a person enters in the text field, but i’m not sure what to set the variable to.

to post a comment
JavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@skywalker2208Oct 09.2008 — show us what you have and we will should be able to help you with what you got.
Copy linkTweet thisAlerts:
@ilovelampauthorOct 09.2008 — i got it wrong, i don't want to set a variable, i just want... well i can't explain it, so here:
[CODE]function binary(frm) {
if (frm.bin.value="2")
alert("10")
frm.Bin.value==""
else
alert("please enter '2'")
frm.Bin.value==""
}
[/CODE]

and I have this as well
[CODE]<INPUT TYPE="TEXT" NAME="Bin">
<input type="button" name="binary" value="Convert" onClick="binary(this.form)">[/CODE]

but when i click the button nothing happens. When i click the button i get no alert boxes, and that is why?
Copy linkTweet thisAlerts:
@justinbarneskinOct 10.2008 — I see, you want to convert base10 numbers to ones and zeros. There is a function but last time I checked, IE doesn't handle it well so, I have not pursued it in awhile. I think it is radix(x) but that isn't it.
Copy linkTweet thisAlerts:
@ilovelampauthorOct 10.2008 — i only need it to work on my computer it's for school
Copy linkTweet thisAlerts:
@skywalker2208Oct 10.2008 — For one thing html should be in capital so it should be like this.
[code=php]
<input type="text" name="Bin">
<input type="button" name="binary" value="Convert" onClick="binary(this.form)">
[/code]


Also I am not 100&#37; sure, but I don't think you could but this.form on the button to get all of the form values. You would have to put it on the form (<form>) element and use the onsubmit function. If you want to use the onclick on the button then you need to use the document.myform.Bin.value to get the value in the text field. You will also need to make sure to give the form element a name. for my example in using the document.myform.Bin.value I am using a form name of myform. I hope that helps.
Copy linkTweet thisAlerts:
@dongzkyOct 10.2008 — i got it wrong, i don't want to set a variable, i just want... well i can't explain it, so here:
[CODE]function binary(frm) {
if (frm.bin.value="2")
alert("10")
frm.Bin.value==""
else
alert("please enter '2'")
frm.Bin.value==""
}
[/CODE]

and I have this as well
[CODE]<INPUT TYPE="TEXT" NAME="Bin">
<input type="button" name="binary" value="Convert" onClick="binary(this.form)">[/CODE]

but when i click the button nothing happens. When i click the button i get no alert boxes, and that is why?[/QUOTE]




i am not sure if it is just a typo error, but you must be confused with when to use "=" and "==" signs. your if comparison is not correct. you say

if (frm.bin.value="2") // only one equal (=) sign.

it should be

if (frm.bin.value=="2") //should be == for comparisons, the correct way

and for your assignment, you say:

frm.Bin.value=="" //"==" is not for assignments, should be "="

the correct way is

frm.Bin.value=""

Another thing, although maybe you have already, make sure that your form objects are inside a form tag (<form name="form_name" >)

And [B]change your function name "binary"[/B] to something else. It should not be the same as the name of your button, this will create a conflict. Just change it to something else like "fn_binary". In all, you will have something like below:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>Untitled</title>

<script>

function fn_binary(frm) {

if (frm.Bin.value=="2")

alert("10");

else

alert("please enter '2'");

frm.Bin.value="";

}
</script>

</head>

<body>

<form name="frmtest" action="form.cfm" method="post">

<INPUT TYPE="TEXT" NAME="Bin">

<input type="button" name="binary" value="Convert" onClick="fn_binary(this.form);">

</form>


</body>

</html>
Copy linkTweet thisAlerts:
@ilovelampauthorOct 10.2008 — thanks for the help, but it's still not working, i've made all the changes you suggested. Anything else?
Copy linkTweet thisAlerts:
@dongzkyOct 11.2008 — Have you terminated each statement with a semicolon( ; )? Can you show us your modified script?
Copy linkTweet thisAlerts:
@ilovelampauthorOct 11.2008 — here it is:
<i>
</i>function fn_binary(frm) {
if (document.frmtest.Bin.value=="2")
alert("10");
else
alert("please enter '2'");

document.frmtest.Bin.value="";

}
Copy linkTweet thisAlerts:
@DokOct 11.2008 — [code=html]
<form>
Dec: <input name="dec" type="text" /><br />
Bin: <input name="bin" type="text" /><br />
<input type="button" value="convert" onclick="this.form.bin.value = (this.form.dec.value * 1).toString(2);" />
</form>[/code]
Copy linkTweet thisAlerts:
@dongzkyOct 11.2008 — you don't have to say document.frmtest.Bin.value="". You can just write it as frm.Bin.value. Anyway, you mean your alert didn't work? what browser are you using? is your javascript enabled? have you checked if no script is blocked by your browser?
Copy linkTweet thisAlerts:
@ilovelampauthorOct 11.2008 — no, i know that alert does work, I have made it work before, I'm using the latest version of safari, I know that javascript is enable and that no script is blocked form my browser
Copy linkTweet thisAlerts:
@DokOct 11.2008 — You want to convert between decimal and binary right? Did you try my example?
×

Success!

Help @ilovelamp 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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