質問

JavaScriptを使用すると、チェックボックスに入力テキストフィールドのテキスト装飾を変更したいと考えています。そのため、チェックされると、入力テキストフィールドのテキストが太字になります。

私が学んでいることを覚えているので、私はあなたたちのようなプロではありません;)

私はこのようなことを考えました。

var checkbox = create("input");
    checkbox.type = "checkbox";
    checkbox.id = "checkboxId" + counter;
    div.appendChild(checkbox);
    checkbox.onClick="boldChange(this)"

var input = create("input");
    input.type = "input";
    input.id = "inputId" + counter;
    div.appendChild(input);



function boldChange()
var boldgroup = document.getElementsByName(el.name);
for (var b=0; boldgroup[b]; ++b)
inputId.style.textDecoration = boldgroup[b].checked ? 'bold' : 'none';
}

どうすればこれを機能させることができますか?よろしくお願いします

役に立ちましたか?

解決

上記のコードに基づいた機能するJSFiddleの例は次のとおりです。 例にリンクします

コードスニペット:(下に置きます </body> すべてのDOMがロードされるように)

<script>
    var div = document.getElementById('div'),
    counter = 0;

    var checkbox = document.createElement("input");
    checkbox.type = "checkbox";
    checkbox.id = "checkboxId" + counter;
    div.appendChild(checkbox);
    checkbox.onclick = boldChange;
    counter++;

    var input = document.createElement("input");
    input.type = "text";
    input.id = "inputId" + counter;
    div.appendChild(input);



    function boldChange() {
        input.style.fontWeight = (checkbox.checked)?'bold':'normal';
    }
</script>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top