Adam Bausy
Adam Bausy

Reputation: 35

Chrome extensions with jQuery

I downloaded http://code.google.com/chrome/extensions/samples.html#ea2894c41cb8e80a4433a3e6c5772dadce9be90d. I would like make it it jQuery, but if i do:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>

<script>
$("div").css('background-color', 'black');

$('.click').click(function(){
  chrome.tabs.executeScript(null,
    {code:"document.body.style.backgroundColor='" + $(this).attr('id') + "'"});
  window.close();
})
</script>
<div class="click" id="red">red</div>
<div class="click" id="blue">blue</div>
<div class="click" id="green">green</div>
<div class="click" id="yellow">yellow</div>

this not working. Nothing happens. Why?

Upvotes: 2

Views: 1513

Answers (3)

Evik James
Evik James

Reputation: 10503

Wrap your jQuery in this:

<script type="text/javascript">
$(document).ready(function() {


});
</script>

So, your jQuery should look like this:

<script type="text/javascript">
$(document).ready(function() {

    // YOUR CODE GOES HERE

})
</script>

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337714

You didn't include the document ready handler, try this:

<script>
    $(function() {
        $("div").css('background-color', 'black');

        $('.click').click(function() {
            chrome.tabs.executeScript(null,
                {code:"document.body.style.backgroundColor='" + $(this).attr('id') + "'"});
            window.close();
        })
    });
</script>
<div class="click" id="red">red</div>
<div class="click" id="blue">blue</div>
<div class="click" id="green">green</div>
<div class="click" id="yellow">yellow</div>

Alternatively you can move your <script> tag to just before the </body> tag so that all HTML is loaded before the javascript.

Upvotes: 3

Sjoerd
Sjoerd

Reputation: 75679

You need to include jQuery first, like

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

You should check the popup page console for errors.

Upvotes: 0

Related Questions