1
1
1styearstudent2019-04-12 12:21:46
Arrays
1styearstudent, 2019-04-12 12:21:46

How to package the process of clicking on a list item more compactly and correctly?

I greet all gurus, guress, gurus and those passing by!
I have a piece of crooked code in js, its essence is simple: when you click on the switch, its value is entered in a hidden input field:

$('#list > li input').click(function () {
    if (checkType('radio') == 1) {
        document.getElementById("field").value = "Значение 1"
    }
    if (checkType('radio') == 2) {
        document.getElementById("field").value = "Значение 2"
    }
    if (checkType('radio') == 3) {
        document.getElementById("field").value = "Значение 3"
    }
});

html piece:
<ul id="list">
    <li>
        <label>
            <input type="radio" name="radio" value="1">
            <span>Значение 1</span>
        </label>
    </li>
    <li>
        <label>
            <input type="radio" name="radio" value="2">
            <span>Значение 2</span>
        </label>
    </li>
    <li>
        <label>
            <input type="radio" name="radio" value="3">
            <span>Значение 3</span>
        </label>
    </li>
</ul>

<input type="hidden" id="field" value="">

The essence of the question: what is the best way to implement such js-code?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Victor L, 2019-04-12
@1styearstudent

D
Dmitry Gordinsky, 2017-02-14
@Sc0undRel

Maybe there are too many versions of jQuery stuffed in, so they conflict.

D
Dark Hole, 2017-02-14
@abyrkov

Most likely, jQuery or one of its plugins has been forgotten. I won’t tell you, because the telepaths dumped everything on vacation

C
Coffee Trip, 2017-02-14
@cfftrp

Have you correctly connected the scripts to WP? Through registration of scripts?
If not by the method recommended by the WP developers, then the scripts can be connected in a different order. Because of this, the conflict.
Function for registration - wp_register_script( params );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question