Hey, I have put the following code into a sharepoint aspx page. So that when I change the value in a drop down box it runs a function (in this case it displays "Works").

<script language="javascript" type="text/javascript">

function getField(fieldType,fieldTitle) {
    var docTags = document.getElementsByTagName(fieldType);
    for (var i=0; i < docTags.length; i++) {
        if (docTags[i].title == fieldTitle) {
            return docTags[i]
        }
    }
}


function TestFunctionName() {

   alert("Works");

} 

getField('Select','DropDownBoxName').onchange = function() {TestFunctionName()};

</script>

What I'd like to be able to do aswell is when a check box is changed or clicked on the same thing happens. I dont seem to be able to achieve it though. I've change the line...

getField('Select','DropDownBoxName').onchange = function() {TestFunctionName()};

to...

getField('Input','CheckBoxName').onchange = function() {TestFunctionName()};

and

getField('Input','CheckBoxName').onclick = function() {TestFunctionName()};


Please could somebody help?

Many Thanks

Recommended Answers

All 3 Replies

Use the 'name' property.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta name="generator" content=
    "HTML Tidy for Windows (vers 25 March 2009), see www.w3.org">
    <title></title>
  </head>
  <body>
    <select name='SelName'>
      <option value="1">
        value=1
      </option>
      <option value="2">
        value=2
      </option>
      <option value="3">
        value=3
      </option>
    </select>
    <form>
      <input type='checkbox' name='CBName'>
    </form>
    <script type="text/javascript">

        function getField(fieldType, fieldTitle) {
            var docTags = document.getElementsByTagName(fieldType);
            for (var i = 0; i < docTags.length; i++) {
                if (docTags[i].name == fieldTitle) {
                    return docTags[i]
                }
            }
        }
        function TestFunctionName() {
            alert("Works");
        }
        getField('Select', 'SelName').onchange = function () {
            TestFunctionName()
        }
        getField('Input', 'CBName').onclick = function () {
            TestFunctionName()
        }

    </script>
  </body>
</html>

Thanks for your reply. Its an aspx from a Sharepoint site.

So the page doesnt have any name properties on the page like

<input type='checkbox' name='CBName'>

This line works perfect for drop down boxes

getField('Select','DropDownBoxName').onchange = function() {TestFunctionName()};

to...

But when I try change it to work with a check box like below it doesnt seem to...

getField('Input','CheckBoxName').onclick = function() {TestFunctionName()};

Thank You

In many contexts name= and id= are interchangeable.
If you have a bunch of elements having neither, unless they belong to a named group or form you are pretty much reduced to trying to figure out the [index] to the TagNames[] collection - but doing so depends on knowing the layout of the page and also knowing that it will never change.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.