/    Sign up×
Community /Pin to ProfileBookmark

if else confusion

I am trying to figure out why this isn’t working … any help appreciated!

<html>

<head>

<title>String</title>

<script language=”javascript”>

function Duh()
{

var str=”The College”
document.write(str.search(/The College/i))
var str = document.myForm.input.value;
document.myForm.output.value = str;
}

if (input == The College)
{
document.write(“<b>Right!</b>”)
}
else
{
document.write(“<b>Wrong</b>”)
}

</script>

</head>

<body>

<form name = “myForm” method=”POST”>
Where do you go to school?: <input type=”text” name=”input”><br>
Output: <input type=”textarea” name=”output” size = 100><br>
<input type=”button” value=”Click Me” onClick=”Duh()”>
</form>

</body>

</body>

</html>

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisJun 30.2006 — JS 101 says that when you envoke document.write() after the page has finished loading, you wipe out the document. Once you wipe out the document, your form no longer exists, and neither does the script.

Moral: don't use document.write() except to generate in-line HTML during load, unless you really, really know what you are doing. ;-)
Copy linkTweet thisAlerts:
@chadillacJun 30.2006 — in otherwords

  • - document.write()


  • + alert();
  • Copy linkTweet thisAlerts:
    @Kathi430authorJun 30.2006 — But doesn't the alert() make a pop up box show up? I don't want one of those ...
    Copy linkTweet thisAlerts:
    @CharlesJun 30.2006 — Use instead: addToDocument (s) {
    t = document.createTextNode (s)
    p = document.createElement ('P')
    b = document.getElementsByTagName ('BODY')[0]

    p.appendChild (t)
    b.appendChild (p)
    }
    Copy linkTweet thisAlerts:
    @chadillacJun 30.2006 — even simpler would be to just update a div's contents

    [code=html]<div id="results"></div>[/code]

    [CODE]
    if (input == college){
    document.getElementById("results").innerHTML = "success";
    }else{
    document.getElementById("results").innerHTML = "failure";
    }
    [/CODE]
    Copy linkTweet thisAlerts:
    @CharlesJun 30.2006 — even simpler would be to just update a div's contents

    [code=html]<div id="results"></div>[/code]

    [CODE]
    if (input == college){
    document.getElementById("results").innerHTML = "success";
    }else{
    document.getElementById("results").innerHTML = "failure";
    }
    [/CODE]
    [/QUOTE]
    But HTMLElement.innerHTML is a yucky MSIE corruption of JavaScript. And it's not even the right yucky MSIE corruption - that would be HTMLElement.innerText. Use instead [code=html]<div id="results">&nbsp;</div>[/code] [CODE]document.getElementById ('results').firstChild.data = input == college ? 'success' : 'failure'[/CODE]
    Copy linkTweet thisAlerts:
    @chadillacJun 30.2006 — Tomato - Tomato ... it works in both FF and IE reliably... thus it works for me ?
    Copy linkTweet thisAlerts:
    @CharlesJun 30.2006 — And the DOM method that I displayed works on both and more and without being yucky.
    Copy linkTweet thisAlerts:
    @chadillacJun 30.2006 — great we both produced results, congrats
    ×

    Success!

    Help @Kathi430 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.29,
    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,
    )...