www.webdeveloper.com
Results 1 to 3 of 3

Thread: Problem while setting dynamic onclick

  1. #1
    Join Date
    Jul 2007
    Posts
    2

    Problem while setting dynamic onclick

    Hi.

    I got some problems while setting dynamic onclick events to img-tags.
    I loop over some fields on a page, these fields have UUIDs as id. Some of the fields will have a img tag connected to them and if they do I want set a onclick function to the img-tag. This onclick function should be called with the field's UUID as an argument. When I use the code below, the same UUID-containing cbMember variable (the last one set in for-loop) is used for all onclicks. Is there any way to avoid this?

    / Helmer

    Code:
    for (var i = 0; i < ccItems.length; i++) {
        var cbMember = ccItems[i].childNodes[0].nodeValue;
        var cbMemberField = document.getElementById("UUID-" + cbMember);
    
        if (cbMemberField != null) {
            cbMemberField.disabled = false;
    
            var pickerImg = document.getElementById("pickerimg_UUID-" + cbMember);
            var format = document.getElementById("format_UUID-" + cbMember).value;
    
            if (pickerImg) {
                if (format == "date") {
                    pickerImg.onclick = function(event) { event = event || window.event; pickDate("UUID-" + cbMember, event); };
                } else if (format == "time") {
                    pickerImg.onclick = function(event) { event = event || window.event; pickTime("UUID-" + cbMember, event); };
                } else if (format == "duration") {
                    pickerImg.onclick = function(event) { event = event || window.event; pickDuration("UUID-" + cbMember, event); };
                }
            }
        }
    }

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Use this.id not "UUID-" + cbMember to refer to the element
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Jul 2007
    Posts
    2
    Brilliant.
    Thank you very much.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles