-1

how to get Numbers (data-id) in text(id) and Numbers (data-id) in text (target) and add for button by (href)

HTML

<span id='a' data-id='111'>id</span>
<span id='a' data-id='321'>target</span>
<button id='send'>send</button>
<br>
<span id='a' data-id='456'>id</span>
<span id='a' data-id='234'>target</span>
<button id='send'>send</button>
</br>
<span id='a' data-id='321'>id</span>
<span id='a' data-id='234'>target</span>
<button id='send'>send</button>
</br>
<span id='a' data-id='213'>id</span>
<span id='a' data-id='123'>target</span>
<button id='send'>send</button>

i use this but Error ? jquery

   $("#a:even").each(function(){
   var b = $(this).attr("data-id");
   });

   $("#a:odd").each(function(){
   var c = $(this).attr("data-id");
   });

   var d = "http://fde&id="+b+"hve&target="+c+";
   $("#send").each(function(){
    $(this).attr("href",d);
    });

but ERROR ? how ?

i want like this

<span id='a' data-id='111'>id</span>
<span id='a' data-id='321'>target</span>
<button id='send' href='http://fde&id=111&hve&target=321'>send</button>
<br>
<span id='a' data-id='456'>id</span>
<span id='a' data-id='234'>target</span>
<button id='send' href='http://fde&id=456&hve&target=234'>send</button>
</br>
<span id='a' data-id='321'>id</span>
<span id='a' data-id='234'>target</span>
<button id='send' href='http://fde&id=321&hve&target=234'>send</button>
</br>
<span id='a' data-id='213'>id</span>
<span id='a' data-id='123'>target</span>
<button id='send' href='http://fde&id=213&hve&target=123'>send</button>

and thanks !!

hokozere
  • 13
  • 3
  • 3
    IDs have to be unique; and you are using the same IDs such as "a" and "send" for multiple elements. – Terry Sep 01 '14 at 01:15
  • You must change to use class instead of id and try again – teddy Sep 01 '14 at 01:29
  • Your code shows a complete lack of understanding of html. If you learn about the difference between id and class you will be able to answer your own question. – JK. Sep 01 '14 at 02:14

2 Answers2

0

Well try this javascript code instead

$('.send').each(function(){
        var id1 = $(this).prev('.a').attr('data-id');
        var id2 = $(this).prev('.a').prev('.a').attr('data-id');
        $(this).attr('href', 'http://fde&id='+id2+'&target='+id1);
}

Hope this will help you and remove all your previous code. Replace your IDs with CLASS too

Ahmad
  • 477
  • 2
  • 9
0

First things first: here is a JSFiddle that does what you're after: http://jsfiddle.net/ftvLkhqg/

But I should mention there are several things fundamentally wrong with your approach that you should consider changing.

  1. IDs must be unique. You have reused the same ID many times in your HTML, you need to consider using "classes" instead (class="a").
  2. 'href' is not a valid attribute on a button element. You are setting the href attribute for the button element, but this is not a valid usage. Consider using a link (<a href=""></a>) instead.
  3. Be careful with loop logic. Your loops are done incorrectly. You've looped through the elements for setting variables 'b' and 'c' before you get to the line where you set the variable 'd'. Due to JavaScript's asynchronous nature you could wind up with any value for 'b' and 'c' at the point where 'd' is set, and 'd' will only be set once. You would wind up with the same URL being set for every button, if it worked at all.
  4. The correct tag to use is <br/> or <br>. Not </br>.

With this in mind, here's a second JSFiddle showing how I might have tackled the same scenario. http://jsfiddle.net/bqgesjjb/3/

Nick Coad
  • 3,623
  • 4
  • 30
  • 63