1

I have some divs and array of inputs when I click on div then input1:last and input2:last must get other values. Can you help me?

    <div class="super" onclick="changeValue(this)">60x90</div>
    <div class="super" onclick="changeValue(this)">100x150</div>
    <input name="input1[]" value="40"/>
    <input name="input2[]" value="50"/>

    function changeValue(o){
        if($('div.super').text() == "60x90") {
        $('input[name="input1[]"]').val('60');
        $('input[name="input2[]"]').val('90');   
        }
    }
1
  • You need to close your name properties with end quotes. Why are you using brackets in the name properties? Also, your function is not closed. Plus, you should try if ($(o).html()=='60X90') { //... change values } Commented Aug 17, 2017 at 20:37

1 Answer 1

1

function changeValue($this)
    {
        console.log($($this).text())
        if($($this).text() == "60x90") 
        {
          $.each($($this).text().split('x'),function(key,value){
            $('input[name="input'+(key+1)+'[]"]').val(value);
           });
       }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="super" onclick="changeValue(this)">60x90</div>
    <div class="super" onclick="changeValue(this)">100x150</div>
    <input name='input1[]' value="40"/>
    <input name='input2[]' value="50"/>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.