Wednesday, July 23, 2014

Javascript and HTML

we see before how to call function in javascript using the HTML elements by the events handlers but how can we use the HTML elements in our javascript code for example if you want to make a simple example of validation for text box

  • the text box shouldnt have a zero value but how can we get the value in our text box 

  • getElementById:
  • this will get the HTML element by its Id we knew that every HTML elements has id,name,value.....etc so its time to use the ID and its very easy the most important thing is there shouldnt be two Id's for two different HTML elements so each element will have its own Id and we'll use that id but we should do it like this 
        Document.getElementById("Element_id");

        and the Document here means our HTML page and all its contents.

  • lets see this example the text box shouldnt have a zero value:


what is "x.value" since HTML elements has more than one attribute so it has id,name,value.....etc we should write what we actually want but that doesnt mean there's "x.id" its only another function that get the value of our HTML elements 
and after we click the button this will happen:

No comments:

Post a Comment