0

I am trying to get onclick/onfocus/onchange in an HTML tag that is being created by Jira. The item itself is a drop down list and while I can get onfocus to work on other IDs, I cant get it to work on the drop down list

What I have:

<script type ="text/javascript" >
console.log("Testing");
var colorDropDown = document.getElementById('someID');

function changeColor()
{
    //if(value)
    alert("Hello World");
}

document.getElementById("someID").innerHTML ="<onfocus=\"changeColor()\"></select>"
//document.getElementById("customfield_11901").innerHTML = "<select class=\"select cf-select\" name=\"customfield_11901\" id=\"customfield_11901\" onfocus=\"changeColor()\">"

</script>

After using innerHTML, the onfocus does not appear in the page. I have also tried this by copying the entire tag and inputting it via HTML.

I have used the .onchange function after getElementById, but that does not work either.

5
  • 1
    <onfocus> isn't a thing. Commented Aug 3, 2016 at 18:25
  • 1
    <onfocus=\"changeColor()\"></select> isn't valid HTML. Do you mean <select onfocus=\"changeColor()\"></select>? Commented Aug 3, 2016 at 18:26
  • 1
    you don't need to inject html to add an event listener, is there a reason you're on that path? Commented Aug 3, 2016 at 18:27
  • @SpencerWieczorek yes, that is what I meant, I will give that try. Commented Aug 3, 2016 at 18:59
  • @aw04 I am just trying things I am seeing as a solution. I will try other things as I find them or as they are suggested Commented Aug 3, 2016 at 18:59

1 Answer 1

3

I would use the .attr() function under jQuery:

$('#select_id').attr('onfocus', 'changeColor();');

Or you can use the addEventListener with plain JS:

object = document.getElementById('#select_id');
object.addEventListener('focus', 'changeColor();');
Sign up to request clarification or add additional context in comments.

1 Comment

I gave this a try and I didnt work at first, but I googled the addEventListener function and made my string look like this: testObject=addEventListener('change', function(){colorChange();},false) This ended up working out for me. Thank you very much!

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.