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?

6 Answers 6

22

Change script to:

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

And inline handler to:

<input type="image" value="" class="but-blue" onclick="btn_click('#col1 p','#00F');" />

update

But with jQuery you can do following:

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

and jQuery script itself:

$(document).ready(function(){
    $('input[type=image]').click(function() {
       var selector = $(this).data('selector');
       var color = $(this).data('color');

       $(selector).css("color", color);
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Since we are using jquery, though, we should bind the click handler to the DOM instead of using the onclick attribute.
Thanks, I've added sample with jQuery.
+1 for showing how to do it right (second part). And you can bind event handlers the right way even without jQuery.
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 :)

Comments

2

something like this? :

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

and the onclick function would be:

<input type="image" value="" class="but-blue" onclick="btn_click('#col1 p','#00f');" />

Comments

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>

Comments

0

Simple as this

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

<input type="image" value="" class="but-blue" onclick="btn_click('color','#00f');" />

Comments

0
$("#redColorBtn").click(function(){
  $("#urTextFiled").css("color", "#f00");
});

$("#greenColorBtn").click(function(){
  $("#urTextFiled").css("color", "#0f0");
});

$("#blueColorBtn").click(function(){
  $("#urTextFiled").css("color", "#00f");
});

# in jquery is used to catch an element on the basis of id value so all # related things should be the id of elements.

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.