AKor
AKor

Reputation: 8882

Trying to use Javascript to populate a hidden input

I have this code:

 while($row = mysql_fetch_array($res)) {
        $name = $row['advertiser'];
        $id = $row['id'];
        if($row['id'] % 4 == 1 || $row['id'] == 1)
        {
            echo "<tr>";
        }
        if($row['availability'] == 0)
        {
            $td = "<td id='green'>";
        }
        if($row['availability'] == 1)
        {
            $td = "<td id='grey'>";
        }
        if($row['price'] == 0)
        {
            mysql_query("UPDATE booklet SET availability = 0 WHERE id = '$id'");
        }
        echo $td . $row['id'] . "<br/><p style='font-size: 6pt;'>" . $name[0] . "<p><img src=" . $row['image'] . "></td>";

        if($row['id'] % 4 == 0)
        {
            echo "</tr>";
        }
}

It creates a table. What I want to do is this: if you click on a td, I want to pass the value of that td, for example - the number one (1), if you clicked on the first td - I want to pass this value over to a hidden input so I may later use it elsewhere.

The table looks fine. I know what to do when I have the value in a hidden input. I just need to be able to have the table, when I click on a td, to pass the value over. Or to do anything. onClick doesn't work. Even the absolute simplest isolated JQuery statements don't even come close to parsing. The most complex Javascript that actually has worked on this page is a document.write(). Everything else stumps any known browser.

So, using absolutely any methods, is it a possibility, within the realm of current technology, to have code that does what I want?

Again, I need to have a table cell, when clicked on, pass a variable to a hidden input. I've tried everything.

Upvotes: 0

Views: 636

Answers (3)

Lie Ryan
Lie Ryan

Reputation: 64837

Try to validate your html, you might have mismatched quotes/square brackets somewhere, which breaks Javascript.

Upvotes: 0

GolezTrol
GolezTrol

Reputation: 116100

<td onclick="document.forms[0]['nameofhiddenfield'].value = this.id"> ... </td>

Upvotes: 1

John K.
John K.

Reputation: 5474

You'll need to add the click event with JQuery and then use Jquery to set the value... Something like this should work.

$(function() {
    $("table#mytable td").mouseover(function() {
    //The onmouseover code
    }).click(function() {
        //The onclick code
        $("#hiddenInputID").val(text);
    });
});

Upvotes: 2

Related Questions