Government Security
Network Security Resources

Jump to content

Photo

Javascript Set A Variable Equal To Getelementbyid?

- - - - - javascript php
  • Please log in to reply
1 reply to this topic

#1 varislost555

varislost555

    Private

  • Members
  • 1 posts

Posted 23 June 2012 - 12:35 AM

Hello guys, how can I set variable firstName equal to getElementById?

I have this:

<script type="text/javascript">

var firstName = *this have to be set equal to getelementbyid(firstName).value*

function nameCheck(){
if(firstName=="Bas"){
alert("Welcome Bas!")
}else{
alert("Welcome somebody else than Bas!")
}
}
</script>

<input type="text" id="firstName">
<input type="submit" value="submit name" onClick="nameCheck()">

I'm using html... not php (I just wanna be sure you know ;-) )
bacterial vaginosis

#2 Booster2ooo

Booster2ooo

    Sergeant

  • Members
  • 247 posts

Posted 24 June 2012 - 02:27 AM


<input type="text" id="firstName">

<input type="submit" value="submit name" onClick="nameCheck()"> <!-- It's not c00l to use inline JS, it's way better to use addEventListener & attachEvent -->



<script type="text/javascript">

    var firstName // Instead of declaring the var here, I would rather declare it in the scope of nameCheck

      , nameCheck = function() {

                        firstName = document.getElementById('firstName').value;

                        (firstName == 'Bas') ? alert('Welcome Bas!') : alert('Welcome somebody else than Bas!');

                    }

      ;

</script>







Also tagged with one or more of these keywords: javascript, php