First button works fine, but the others are out of order.

Discussion in 'JavaScript' started by sznczy, Aug 10, 2011.

  1. #1
    I have this code:

    common.js
    This is integrated in index.php:
    header:
    Body:
    The first button works fine, but others do not work. What shall I do?
     
    sznczy, Aug 10, 2011 IP
  2. astrazone

    astrazone Member

    Messages:
    358
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #2
    First of all id is suppose to be unique.. so use <input class="mizuButton"
    try this
    
    $(".mizuButton").each(function(){
        $(this).click(function(){
             var ertek = $(this).attr("rel");
             //continue ajax code here...
        })
    })
    
    
    PHP:
     
    astrazone, Aug 10, 2011 IP
  3. sznczy

    sznczy Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I use this:
    common.js
    index.php
    This looks like good, but the button works one after the other. For example: when I would like to click on Téma3, I have to click before this action on Téma1 and Téma2 too.
    What shall I do?
     
    sznczy, Aug 10, 2011 IP
  4. astrazone

    astrazone Member

    Messages:
    358
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #4
    index.php
    
    <input class="mizuButton"  type="button" name="mizu" value="Téma1" rel="s" />
    <input class="mizuButton" type="button" name="mizu" value="Téma3" rel="123" />
    <input class="mizuButton" type="button" name="mizu" value="Téma2" rel="5" />
    <input class="mizuButton" type="button" name="mizu" value="Téma4" rel="555" />
    
    PHP:
    javascript :

    
    $("input .mizuButton").each(function(){
    //find every input that has .mizuButton Class 
    $(this).click(function(){
    //for every button set up a listiner for a click
    var ertek = $(this).attr("rel");
    //$(this) is the button jquery object. it changes everytime on diffrent clicks
    $.ajax({
    url: "./mizu.php",
    data: "mizu="+ertek,
    type: "post",
    success: function(h) {
    $("#content").html(h);
    }
    });
    
    PHP:
    this is the only thing you need...
    try it and tell me if it worked :)

    good luck
     
    astrazone, Aug 10, 2011 IP
  5. sznczy

    sznczy Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks, but this code doesn't work.
    I don't understand this. :(
     
    sznczy, Aug 11, 2011 IP