How to increment number on click by using javascript / jquery? -



How to increment number on click by using javascript / jquery? -

i have 5 links gets created in loop ($.each):

$.each(data, function(index, element) { name = element.name; id = element.id; $('<a href="#" id="'+id+'" onclick="submitnotification('+id+');">'+name+'</a>') });

in case create 5 links:

<a href="#" id="1" >name1</a> <a href="#" id="2" >name2</a> <a href="#" id="3" >name3</a> <a href="#" id="4" >name4</a> <a href="#" id="5" >name5</a>

and

function submitnotification(cdata){ alert('you selected '+cdata->name+' on '+place+'place'); $.ajax({ type: 'post', url: 'http://www.example.com/test.php', data: cdata, datatype:'json', success: function (data) { ... } }); }

what want when click on link alert: you selected name2 on 1 place. if click link same alert place var incremented one.

in other words each time click on link place starting @ 1 , increment 1 until gets 5

then send info ajax post.

now, there 2 things have dificulty doing:

1. placing id , name within javascript object both can available send ajax post 2. how do increment no matter on link click first countwill start 1.

any ideas? knowing help me lot.

thanks

declare place counter , info container outside function:

var place = 0, postdata = []; function submitnotification(cdata){ place++; alert('you selected '+cdata->name+' on '+place+'place'); postdata.push(cdata); if (place == 5) { $.ajax({ type: 'post', url: 'http://www.example.com/test.php', data: { places: postdata }, datatype:'json', success: function (data) { ... } }); } }

javascript jquery ajax arrays increment

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -