Alex Rivera | Logout

Passing parameters to a jquery function from a button click

Asked 2011-12-09T07:51:05.457
13

I have to be able to change the colour of text via three buttons (red, blue and black)

The function I have, which works fine is as follows:

function btn_click() {
$("#col1 p").css("color", "#00f");
    }

I need to pass two variables to this function, the specific tag (e.g. col1 p) and the hex value from the following function call:

<input type="image" value="" class="but-blue" onclick="btn_click();" />

Can anyone help?

Edit
Report

2 Answers

5

If you want to go full-JQuery, do the Following

function btn_click(selector, color) {
    $(selector).css("color", color);
}

$("input").bind("click", function(event) {
    btn_click($(this).attr("goal-selector"), $(this).attr("goal-color"));
});

And then in your HTML

<input type="image" value="" class="but-blue" goal-selector="#col1 p" goal-color="#00f" />

Good luck :)

answered 2011-12-09T08:02:13.687
2

Aren't this be better:

<input type="button" value="#00F" class="button-color"  />
<input type="button" value="#0FF" class="button-color"  />
<input type="button" value="#FFF" class="button-color"  />
<div id="you_need_to_change"></div>
<script type="text/javascript">
$('.button-color').click(function(){$('#you_need_to_change").css("color", $(this).val())});
</script>
answered 2011-12-09T08:03:33.467

Your Answer