Anjelica Anjelica - 22 days ago 6
jQuery Question

Get value of dynamically added button

I am building a simple questionnaire application.

My current function retrieves the options of each question and append them as buttons. I would like to code another function preparefornextquestion to alert the value of whichever button is selected. The current one does not seems to get the value of whichever is clicked. :(



$('#option_one').append("<button data-role='button' class='option' data-inline='true' data-mini='true' data-theme='b' onclick='preparefornextquestion()' value='" + arr[0].option_one + "'>" + arr[0].option_one + "</button>").trigger('create');

$('#option_two').append("<button data-role='button' class='option' data-inline='true' data-mini='true' data-theme='b' onclick='preparefornextquestion()' value='" + arr[0].option_two + "'>" + arr[0].option_two + "</button>").trigger('create');

$('#option_three').append("<button data-role='button' class='option' data-inline='true' data-mini='true' data-theme='b' onclick='preparefornextquestion()' value='" + arr[0].option_three + "'>" + arr[0].option_three + "</button>").trigger('create');

function preparefornextquestion(){


$(document).ready(function() {
$('.option').click(function() {
alert($(this).attr("value"));
});
});
}

<div id="option_one"></div>
<div id="option_two"></div>
<div id="option_three"></div>




Answer
$('#option_one').append("<button data-role='button' class='option' data-inline='true' data-mini='true' data-theme='b' onclick='preparefornextquestion(this)' value='" + arr[0].option_one + "'>" + arr[0].option_one + "</button>").trigger('create');

$('#option_two').append("<button data-role='button' class='option' data-inline='true' data-mini='true' data-theme='b' onclick='preparefornextquestion(this)' value='" + arr[0].option_two + "'>" + arr[0].option_two + "</button>").trigger('create');

$('#option_three').append("<button data-role='button' class='option' data-inline='true' data-mini='true' data-theme='b' onclick='preparefornextquestion(this)' value='" + arr[0].option_three + "'>" + arr[0].option_three + "</button>").trigger('create');


function  preparefornextquestion(val){
alert($(val).attr('value'))
}
Comments