0
function displayResult1(szybkosc) {
    var n = value = szybkosc;
    var u = n.split("|")[1]; 
    document.getElementById('result1').innerText = ' '+u;
}

function displayResult2(szybkosc) {
    var n = value = szybkosc;
    var u = n.split("|")[1];
    document.getElementById('result2').innerText = ' '+u;
}

The JavaScript code works but I need to do new function for each result, is it anyway to make it quicker?

$i = 1;
while($i < 10){
    $result = "result" . $i;
    $displayResult = "displayResult" . $i;
    echo "user number ".$i."
    <input type='radio' name='szybkosc' onclick='$displayResult(this.value)' value='$sp1[id]|1|$row[id]' />
    <input type='radio' name='szybkosc' onclick='$displayResult(this.value)' value='$sp1[id]|3|$row[id]' />
    <span id='$result'></span></br>
    <input type='radio' name='ss' value='$sp1[id]|1|$row[id]' />
    <input type='radio' name='ss' onclick='$displayResult(this.value)' value='$sp1[id]|1|$row[id]' />";
    $i++;
}
0

2 Answers 2

1
  1. Why do you have two functions for the same purpose?
  2. What is the purpose of using value here var n = value = szybkosc;?
  3. Your code does not have matching braces and proper html tags.

Just pass the id as the second parameter:

function displayResult(szybkosc, id) {
    var n = szybkosc;
    var u = n.split("|")[1];
    document.getElementById(id).innerText = ' ' + u;
}

The PHP Code is updated as:

$i = 1;
while($i < 10){
    $result = "result" . $i;
    $displayResult = "displayResult" . $i;
    echo "user number ".$i."
    <input type='radio' name='szybkosc' onclick='$displayResult(this.value)' value='$sp1[id]|1|$row[id]' />
    <input type='radio' name='szybkosc' onclick='$displayResult(this.value)' value='$sp1[id]|3|$row[id]' />
    <span id='$result'></span></br>
    <input type='radio' name='ss' value='$sp1[id]|1|$row[id]' />
    <input type='radio' name='ss' onclick='$displayResult(this.value, "result' . $i .'")' value='$sp1[id]|1|$row[id]' />";
    $i++;
}
Sign up to request clarification or add additional context in comments.

2 Comments

It doesn't work, shouldn't be document.getElementById(result, id) instead document.getElementById(id) maybe?
No. getElementById() is an in built function that accepts the string representation of the id as a parameter. What error are you getting. Check the console in developer tools / Firebug.
0

You don't need to use multiple js functions for each of the items in your php loop. Just use this single js function and pass parameters to it accordingly:

function displayResult(szybkosc, id)
   var n=value=szybkosc;
   var u=n.split("|")[1]; 
   document.getElementById('result'+id).innerText = ' '+u;
}

And modify the php loop code that calls the js function:

$i = 1;
    while($i     $result = "result" . $i;
    $displayResult = "displayResult" . $i;
    echo "user number ".$i."
    input type='radio' name='szybkosc' onclick='$displayResult(this.value, '.$i.')' value='$sp1[id]|1|$row[id]' />
    input type='radio' name='szybkosc' onclick='$displayResult(this.value, '.$i.')' value='$sp1[id]|3|$row[id]' />  
   input type='radio' name='ss' value='$sp1[id]|1|$row[id]' />
   input type='radio' name='ss' onclick='$displayResult(this.value, '.$i.')' value='$sp1[id]|1|$row[id]' />";
   $i++;
}

By the way, your php while loop has no opening brace.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.