Solved

Catch and format to json with Jquery

Posted on 2015-01-08
2
30 Views
Last Modified: 2016-05-12
Good day people, i'm having some problems with collecting the data i am passing via jquery's ajax post method.
So far i achieve to connect to the backend servlet with no parameters. No i must pass the data collected in a json (or any avaible) format.

the tags in html are the following

<ul class="user-tags"> 
<li value="124506">
<span class="acc">100</span>
<span class="skill">java</span>
<div >
	<input class="btn" type="button" value="+" />
</div>
<span>
	<strong>
		other stuff
	</strong>
</span>
</li>
<li value="24869701">
	<span class="acc">105</span>
	<span class="skill">sql</span>
	<div>
		<input class="btn" type="button" value="+" />
	</div>
	<span>
	<strong>
		other stuff
	</strong>
	</span>
</li>
</ul>

Open in new window


I would like to send those <span> when the proper button is clicked, if possible on json or any other viable format.
each <li> element has a value, and a <span> with the values i want to send....

how do i collect the clicked row values with jquery???
i am not a ninja on front-end things.. yet!
0
Comment
Question by:JavierVera
2 Comments
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 40539280
This should get you started: http://jsbin.com/zewige/1/edit?html,js,output

I've also changed the 'value' attribute of the <li> tags to a data attribte as you can't have 'value' as an attribute

var items = [];

$(function() {
  $('.btn').on('click', function(e) {
    var me = $(e.target);
    // disabled the button to allow only once
    me.prop('disabled', 'disabled');
    
    var parent = me.parents('li');
    
    items.push({
      acc: parent.find('.acc').text(),
      skill: parent.find('.skill').text(),
      val: parent.data('value')
    });
    
    console.dir(items);
    
  });

  $('#submit').on('click', function() {
    $.ajax({
      url: '/your/url',
      data: {
        values: JSON.stringify(items)
      }
    });
  });
});

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now