vbCoder
vbCoder

Reputation: 75

change attribute of a selected element value dynamically from jquery

Lets say I have two elements inside a parent container.

<div class="myclass" id="myid" myvalue="1"></div>
<div class="myclass" id="secid" myvalue="2"></div>

Now I select firstitem, I set the value from spinner and code will be something like selectable plugin but I want to assign values to selected item.

  $('#myspinner').spinner({
        stop: function (event, ui) {
            //change myid myvalue to selected value
        }
    });

How can I change the value of the second element using the same code? My fiddle my example

Upvotes: 0

Views: 113

Answers (2)

Nikhil Talreja
Nikhil Talreja

Reputation: 2774

<div class="myclass" id="myid" myvalue="1"></div>
<div class="myclass" id="secid" myvalue="2"></div>

var spinner = $("#spinner").spinner();
    var selectedDiv = '';

    $('.myclass').click(function () {
        selectedDiv = this;
    });

    $('#myspinner').spinner({
        stop: function (event, ui) {
            $(selectedDiv).html(this.value);
        }
    });

The selectedDiv variable will hold the element that has been selected by clicking. When the spinner is stopped, the stop: function is triggered. This will in turn set the html content of the selected div to the value selected using the spinner (this.value)

Upvotes: 2

Your posted code is incorrect. You might want to check it out and re-post it.

If you want a click event to happen when firstitem is clicked then your selector should be $('.firstitem') and not $('#myspinner').

You'll need to make those changes to begin with.

Upvotes: 0

Related Questions