Psl
Psl

Reputation: 3920

Jquery to get the value on radio button click

In the radio button click i have to get the customparamvalues.For that on the onclick of radio button i write the following code

function myfunc(ele) {

    var $tr = $(ele).parent().parent();
    var name =$tr.find("td:eq(2)").html();    //alerts name its getting   
    var scripts = $tr.find("td:eq(3)").html(); //alerts scripts  its getting 

   alert("Custom Param values.........."+$tr.find("td:eq(4)").html())
// it alerts my values 1:ONE<br>2:TWO<br> 3:THREE<br>4:FOUR

}

so in $tr.find("td:eq(4)").html() i got in the following format

1:ONE<br>2:TWO<br> 3:THREE<br>4:FOUR

What i want is i have to output values in the following format how it is possible...

 var val1 = 1;
  var  val2 =ONE

var val3=2
var val4=Two
..........
.............

my html code

joblist = joblist + "<table class='tablestyle'><tr><th class='thstyle'>Select</th><th class='thstyle' id='td_1'>Id</th><th class='thstyle'>Name</th><th class='thstyle'>Test Script</th><th class='thstyle'>CustomParamValues</th></tr>";


var tabString = '<tr ><td align="center" class="tdstyle"> ' + '<input type="radio" name="joblist" onclick="myfunc(this);"  id= ' + value._id + 'value=' + value._id + '> </td><td id="td_1" align="center" class="tdstyle">' + value._id + '</td><td align="center" class="tdstyle">' + value.names + '</td><td align="center" class="tdstyle">' + value.script + '</td><td class="tdstyle">' + value.CustomParam1key + ':' + value.CustomParam1Value + '<br>' + value.CustomParam2key + ':' + value.CustomParam2Value + '<br> ' + value.CustomParam3key + ':' + value.CustomParam3Value + '<br>' + value.CustomParam4key + ":"+ value.CustomParam4Value + '</td></tr>';

                    joblist = joblist + tabString;

Upvotes: 0

Views: 264

Answers (1)

bipen
bipen

Reputation: 36551

considering your string is alway in this format 1:ONE<br>2:TWO<br> 3:THREE<br>4:FOUR

instead of creating a variable , you can push the text to an object or array and use that later. using split() to split the html(text).

for example:

var obj = [];
var data = $tr.find("td:eq(4)").html();
data1 = data.split('<br>');
for (var i = 0; i < data1.length; i++) {
  datas2 = datas[i].split(':');
  for (var j = 0; j < datas2.length; j++) {
    obj.push(datas2[j]);
  }
}

here, obj will have all the values you need..

 alert(obj[0]) //gives 1
 alert(obj[1]) //gives ONE
 alert(obj[4]) //gives 3 ..soon

however, this is one of many ways to get what you want...though using two for loop is not good

fiddle here

Upvotes: 1

Related Questions