SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2012
    0 Post(s)
    0 Thread(s)

    Javascript simple code not working on Content form

    Howdy folks,

    New to Javascript programming.

    I was able to do decent javascript (getting refereces to various elements on a page and read attributes or set attributes)...but when I try to do same kind of programming in a content page, it misbehaves.

    For example a have a table element on the content page and button. in the onclick event of the button control, I am getting the <td> element and change its colors. but for some reason instead of changing the color of just the <td> element, the entire content page as well as Master page changes.

    can someone guide me please.

    <%@ Page Title="" Language="C#" MasterPageFile="~/DMS.Master" AutoEventWireup="true" CodeBehind="Javascript_RetrievingByTagName.aspx.cs" Inherits="DMS.WebUI.Javascript_RetrievingByTagName" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
    <script type="text/javascript">
        function changecolors() {
            var a1 = document.getElementsByTagName("td");
            var a1Length = a1.length;
            for (var i = 0; i < a1Length; i++) {
                a1[i].style.background = "#aaabba";
    <table id="mytable" border="1">
        <td id="lefttd0">Left column</td>
        <td id="righttd0">Right column</td>
        <td id="lefttd1">Left column</td>
        <td id="righttd1">Right column</td>
        <td id="lefttd2">Left column</td>
        <td id="righttd2">Right column</td>
    <a href="#" onclick="return changecolors();">Click to Change Colors</a>
    <input id="Button1" type="button" value="button" onclick="changecolors();"/>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Christchurch, New Zealand
    104 Post(s)
    4 Thread(s)
    Quote Originally Posted by bnath001 View Post
    can someone guide me please
    Sure thing.

    It works for me when I place some appropriate parts in to an HTML page, so we need to find out what else may be interfering with things.

    What is the HTML code of the page that's rendered from your ASP code?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts