0

I have a website which loads from an ajax code and I have an language select image link which I want to add an onclick event to. The onclick event has to be added to the <a> tag. When the page loads in the browser, it forms the html code as

<a class="whatever">
<div id="1234" class="whatever1" width: 25px; height: 25px;">
<img class="whatever2" alt="" src="imagelink.png" width: 25px; height: 25px;">
</div>
</a>

Since I cannot change the html code manually (I could add javascript into the page), how could I load this: onClick="SetCookie('Language','En');document.location.href='http://example.com/';" into the <a> tag ?

Could I use the class in the <a> tag to target the element and load the onclick event into it somehow?

Thanks in advance for any help.

7
  • Are you using JQuery? Commented Feb 24, 2014 at 4:02
  • I tried using jquery just like Subash Selvaraj told me to except jquery makes my website malfunction. I guess I can't use it, I'll have to use javascript. Commented Mar 6, 2014 at 23:13
  • JQuery is javascript. Commented Mar 7, 2014 at 1:16
  • I know that jquery is javascript but it has too much coding packed together that it messes with how my website loads. That's why I need something a lot simpler. Commented Mar 7, 2014 at 17:29
  • If JQuery is "messing with how your website loads" then I think there is probably some very bad practices happening in the javascript on your website. JQuery is entirely namespaced and you can even turn off the $ function by calling jQuery.noConflict() right after jquery is loaded. Commented Mar 7, 2014 at 19:27

1 Answer 1

0

use Jquery. here whatever should be replaced with your <a>'s class name.

$("body").on("click",".whatever",function(){
    SetCookie('Language','En');
    document.location.href='http://example.com/';
});
Sign up to request clarification or add additional context in comments.

3 Comments

I posted a script code in the header but it seems like it didn't work. This is what I put: <script> $("body").on("click",".CertainClassName",function(){ SetCookie('Language','English'); document.location.href='http://www.example.com'; }); </script> I also tried a few suggestings from this post: (stackoverflow.com/questions/12284168/…)
have you included jquery in your code? <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> or <script src="cdnjs.cloudflare.com/ajax/libs/jquery/2.0.1/jquery.js"></script>
I tried using jquery and it makes my website malfunction(and the code doesn't appear after the page loads). It would be nice if there was another solution to inject or insert the html code after the page loads.

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.