Get latest news and Web Designing tutoria

Thursday, 4 June 2015

Using getElementById in Javascript

The document method getElementById is used to manipulate particular HTML elements on your page. As its name suggests, though, the HTML element needs an ID attached to it. As an example, take a look at this segment of a web page:



So we have a textbox and a button. When the button is clicked, we want to make sure that the textbox has been filled in. If it's still blank then we don't want to send the form anywhere. We can keep the user on the same page and colour the textbox, say, red. This will offer a visual clue that the textbox needs to be filled in. Here's what the textbox will look like if it's left blank and the button is clicked:



The code for all this makes use of getElementById. Here it is:

Use of getElementById in Javascript

In the BODY section of the HTML we have a textbox and a button. But notice the highlighted ID for the textbox:
ID="textbox_1"
So we've attached an ID to this HTML element. We then use this ID with the following: (Don't worry about the IF Statement - you'll study those soon.)
if ( document.getElementById("textbox_1").value=='' ) {
}
The getElementById method needs a pair of round brackets. In between these round brackets you type the ID you're trying to access. The ID needs to be surrounded by quote marks, single or double. Textboxes have a VALUE attribute. You can get at this value by adding it after the round brackets (type a dot first, though). We then have two equal symbols. To specify a blank value in the text box, we have two single quote marks (we could have used doubles, though).
One thing to be aware of for getElementById is that the "d" of "Id" is lowercase but the "I" is uppercase. So it's not "ID" but "Id". Get that bit wrong and your code won't work.
To change the textbox red, our code is this:
document.getElementById("textbox_1").style.backgroundColor='red';
So we are still accessing the ID of the textbox. This time, we're adding a style after the round brackets:
.style.backgroundColor='red'
The particular style we're changing is the background colour.

Related Posts:

  • Javascript Form Validation - the email function We'll start with the function that checks the email address text box. We'll only check if the text box has had anything typed into it. Checking for a correct email address is notoriously difficult, so we're just trying to k… Read More
  • Checkboxes The final function we need is for the checkbox. You can have 1 or more checkboxes on a form. We only have 1, so checking it is fairly straightforward. If you have more than checkbox, though, you do the testing in exactly the… Read More
  • Radio Buttons Checking radio buttons on a form is a little trickier. You have to loop through each button, testing for a value of checked, which means it was selected. Only one radio button in a group can be selected, so if you find… Read More
  • Dropdown Lists The code to check the dropdown list is more or less the same as for radio buttons. Here it is: The only difference is in the IF Statement. Between the round brackets we have the following: document.frmOne.hear[i].select… Read More
  • Using getElementById in Javascript The document method getElementById is used to manipulate particular HTML elements on your page. As its name suggests, though, the HTML element needs an ID attached to it. As an example, take a look at this segment … Read More

0 comments:

Post a Comment