holeN1
holeN1

Reputation: 13

Change field id dynamically in javascript

I have a checkbox and a label for the checkbox that is actually coded as another field:

    <td style="text-align:left; width:210px; font-size:11px;
    height:20px;vertical-align:middle; " colspan="2" >
    <asp:CheckBox ID="Weaknessx" runat="server" AutoPostBack="false"/>
    <a style="color: black;text-decoration: none;" id="Weakness"
       href="javascript:void(0)" 
       onclick="addlinethrough(Weakness)"
       ondblclick="removelinethrough(Weakness)">Weakness</a>
    </td>

There are options for the user: click the checkbox / unclick the checkbox; click the label / unclick the label

When clicking the label the textDecoration is changed to line-through. When dblclicking the label the textDecoration is changed to none. This works fine.

What I want to do is when clicking the label to put line-through I also want to "uncheck" the checkbox. There are several similar fields so to do this with one set of javascripts I have named the labels and the checkboxes similarly. label id = "Weakness" checkbox id = "Weaknessx" The checkbox always has an appended "x".

This is my javascript which is not working. The checkbox is not getting unchecked.

function addlinethrough(elem) {
    elem.style.textDecoration = "line-through";
    document.getElementById(elem+x).checked = false;
    return false;
}
function removelinethrough(elem) {
    elem.style.textDecoration = "none";
    return false;
}

It appears to me that the checkbox id is not being recognized

Upvotes: 1

Views: 70

Answers (1)

baao
baao

Reputation: 73291

If you want to append a string you need to use ticks and you need to get the ID, not the element itself:

document.getElementById(elem.id + 'x')

will work.

Upvotes: 1

Related Questions