Help, please, understand, I can not get the value of onclick .

There is a link of the form

 <span class="flist-values" id="flist-values-other2"> <a href="javascript://" onclick="return categoryFilter('other2',2)">да</a> </span> 

How can I pull onclick out of it?


ANSWER:

(thanks to everyone who helped!)

 $('document').ready(function () { var i = 0; // это цикл по всем элементам .flist-item $('.flist-item').each(function(){ i++; // получаю значение onclick в переменную text var text = $(this).find('a').attr('onclick'); // искомая строка - название функции, скобка, кавычка :) var search = 'categoryFilter(\''; // нахожу позицию, с которой начинаются аргументы var argsStart = text.indexOf(search)+search.length; // получаю аргументы text = text.substring(argsStart, text.indexOf(')', argsStart)); // получаю массив из двух элементов var result = text.split('\',') // это увидите в консоли браузера cf1 = result[0]; cf2 = result[1]; var htmlrepl = '<input type="checkbox" id='+i+' name='+cf1+' value='+cf2+'>'; $("#flist-values-other"+i).html(htmlrepl); }); }); 
  • What exactly do you want to get / pull out of onclick? return is needed to prevent further processing, i.e. lest he go over href. - IVsevolod
  • categoryFilter ('other2', 2) both variables in the function can change ... - Dimastik86
  • @ Dimastik86 instead of i ++ you can use the loop index, .each (function (index) { - lampa

5 answers 5

And you can still try this:

 $('document').ready(function () { // это цикл по всем элементам .flist-item $('.flist-item').each(function(){ // получаю значение onclick в переменную text var text = $(this).find('a').attr('onclick'); // искомая строка - название функции, скобка, кавычка :) var search = 'categoryFilter(\''; // нахожу позицию, с которой начинаются аргументы var argsStart = text.indexOf(search)+search.length; // получаю аргументы text = text.substring(argsStart, text.indexOf(')', argsStart)); // получаю массив из двух элементов var result = text.split('\',') // это увидите в консоли браузера console.log(result[0]); // первый аргумент console.log(result[1]); // второй аргумент console.log('-----'); // просто разделитель }); }); 

This code passes all .flist-item elements (see each ()), searches there for a link, gets the onclick content; After that, categoryFilter(' finds it in it and takes everything after it, but before the next closing bracket. Then divides the line into two.

But the problem is in the formation. The best solution would be to transfer these parameters in some more accessible way.

  • it’s complicated for me so far ((I don’t understand what to assign in your code ((tell me how to just pull out categoryFilter from the link ('and take everything after it, but before the next closing bracket. and assign values ​​to two variables cf1 and cf2 - Dimastik86
  • @ Dimastik86, see the comments in the updated answer. Instead of console.log.. write what you want: the arguments are stored in result You can: cf1 = result [0]; cf2 = result [1]; - xEdelweiss
  • but how when using each () to count the circles of cycles? - Dimastik86
  • Declare a variable ( var i = 0; ) outside each() , and inside increase the value ( i++ ) - xEdelweiss

Maybe you wanted it?

 <span class="flist-values" id="flist-values-other2"> <a href="javascript://" onclick="location.href = categoryFilter('other2',2); return false;">да</a> </span> 
  • no, see above ... - Dimastik86
 $('#flist-values-other2').on('click', function() { var onclick = $(this).attr('onclick'); alert(onclick); }) 
  • does not work, writes null jsfiddle.net/Dimastik86/ZSzPW/1 $ ('document'). ready (function () {var countCH = $ (". flist-label"). length; for (i = 1; i < = countCH; i ++) {$ ('# flist-values-other' + i) .on ('click', function () {var onclick = $ (this) .attr ('onclick');}) var htmlrepl = '<input type = "checkbox" onclick =' + onclick + '>'; $ ("# flist-values-other" + i) .html (htmlrepl);}}); - Dimastik86
  • @ Dimastik86 your logic and code are not correct. Getting the value onclick occurs only when you click. Accordingly, nothing will happen before. Another couple of minor amendments introduced, see: pastebin.com/kHhV37Cw - lampa
  • Your code in general does not work for me in the way it was planned, besides, onclick it doesn’t look, what’s wrong, why can't I get the value? jsfiddle.net/Dimastik86/ZSzPW/2 - Dimastik86
  • @ Dimastik86 so? jsfiddle.net/ZSzPW/4 - lampa

See an example here . ( choose the right selectors! )

    and kakzhe

     alert($("#flist-values-other2").attr("onclick")); 
    • alert ($ ("# flist-values-other1"). attr ("onclick")); the result is null - Dimastik86