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.
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();');
Related
I found some JQuery solutions, but I am limited by school task restrictions to use pure Javascript, and I need to use specific early appended element that is still not in DOM for replacing by my CKEDITOR.
Code:
function newOption(){
...
mainUL = document.getElementById("myUL");
var inputA = document.createElement("input");
inputA.type ="text";
inputA.style = "margin-right: 45px";
inputA.name = "option[]";
inputA.id = inputID;
mainUL.appendChild(inputA );
CKEDITOR.replace(inputID).setData('Type anything you want ...');
...
}
By replacing my input with CKEDITOR will JS fail, because input, commonly, is still not in DOM. I tried to use
mainUL.innerHTML += "all elements like html text";
and this is working and will immediately insert elements into DOM, but I can't to use innerHTML, because it will remove old listeners (for example checked checkboxes that JS will set from checked to unchecked, what is my main problem due to I have to try using append DOM function).
Try changing the code to wrap the call to CKEDITOR.replace in a setTimeout:
setTimeout(function() {
CKEDITOR.replace(inputID).setData('Type anything you want ...');
},0).
This will allow the browser time to insert the element before trying to replace it.
And I assume that inputID has a valid value in it...
I'm just beginning to learn Javascript and I'm currently trying to use it to change the HTML of an anchor tag. I'd rather not do this by each ID as I will eventually have a few of the same anchors so I wanted to do it with a class.
<div class="testing2">
Customize
</div>
<script type="text/javascript">
var test = document.getElementsByClassName('testing2');
var anchors = test.getElementsByTagName('a');
if (anchors[0]) {
anchors[0].innerHTML="Blank";
}
</script>
Problem is, I can make this work when using an id but not when using the class - I receive the error Uncaught TypeError: test.getElementsByTagName is not a function.
What am I missing?
You can simply use querySelector to achieve this as
document.querySelectorAll('.testing2 a')[0].innerHTML
The problem here is that test contains an array but you're not calling it as one.
var anchors = test[0].getElementsByTagName('a');
1st you need querySelectorAll('.testing2 a') to select all a element inside the element with class .testing2
2nd you need a forEach loop to loop through all the element selected and process to update innerHTML to Blank.
<div class="testing2">
Customize1
Customize2
Customize3
Customize4
</div>
<script type="text/javascript">
var testing2 = document.querySelectorAll('.testing2 a');
Array.prototype.forEach.call(testing2, function(el) {
el.innerHTML = "Blank";
});
</script>
Background My page creates a list of objects based on rows of an SQL Database. For each object, a DIV is dynamically generated that contains a few items including a LinkButton and a further child DIV that is initially hidden. I want the link button to toggle the child DIV's hidden property. The JavaScript is not dynamically generated and is included in the ASPX page.
Problem I don't know how to make this generated LinkButton fire JavaScript that is included in the ASPX page and pass in the correct DIV's ID.
I'm guessing I need to add an attribute to the button like so:
myButton.Attributes.Add(reference to JS function + parameter of DIV's ID)
Maybe like:
myButton.Attributes.Add("onclick", "Show_Hide_Display('"<%="' +idString+ '".ClientID%>"')");
Where the button is given an attribute of a JS onClick handler pointing to the function "Show_Hide_Display" and a parameter of a DIV's ID that is calculated as the rendered ID. This syntax is incorrect though.
How do I write this so it calls 'Show_Hide_Display' and passes the ID of the current child DIV? All of the DIVs have the same ID apart from a number that references their row number, for example '"myDiv_" + counter.ToString'
The JavaScript I am trying to add a call to on the button:
function Show_Hide_Display(divID) {
var div = document.getElementById(divID);
var style = document.defaultView.getComputedStyle(div);
var display = style.getPropertyValue('display');
if (display == '' || display == 'block') {
div.style.display = 'none';
} else {
div.style.display = 'block';
}
}
Use the following syntax ...
myButton.Attributes.Add("onclick", "Show_Hide_Display(this.id);");
the above syntax allows to call the function with id as its parameter.
suggestion:
Try to write a common function which does not depend on generated ids of controls.
If this is not useful for your requirement, please post your code which might gives me a better idea.
If you are using jQuery, you could you jQuery delegate method.
$(document).on("click", "div.parent", function(){
var subDivId = getSubDivByParent(this);
Show_Hide_Display(subDivId);
};
You need to implement getSubDivByParent according your DOM structure.
If you are not using jQuery, you need to attach event yourself. For each dynamically generated element. You need to manually add following script in your server code to register event.
... your html code ...
<script>
var elem = document.getElementById('new-created-element');
elem.addEventListener("click", function(){
var subDivId = getSubDivByParent(this);
Show_Hide_Display(subDivId);
};)
</script>
My suggestion is use jquery to achieve the functionality.
My solution works if you want to toggle the immediate div for the link.just call onclientclick method to toggle the div.
in linkbutton onclientclick="Show_Hide_Display(this)"
function Show_Hide_Display(id) {
$(id).next('div').toggle();
}
I hope this helps you .. Thanks
i have this pretty simple piece of code where i get a certain id and what im trying to do is to change the element's css.
the code looks like this:
<script>
jQuery('.sitePick').click(function(event){
var site = (event.target.href.split("#")[1]); // get the id
console.log(site); // make sure i get the correct id
jQuery('#' + site).css("display" , "block"); //change css
});
</script>
the log prints the correct id of the element, and when i tried t use the same id like this : docuemnt.getElementById(site).style.display = "block"
it worked, but not when using jQuery.
what worries me the most its that even when i passed the id as is ( jQuery("#dummySite") to jQuery it still didn't work..any idea what i'm doing wrong? thx
UPDATE
the value of site is "site_buzzy.com".
I am loading jquery right at the beginning of the file, other jquery methods work.the script being loaded after the content.
You need to escape dot(s) in some way to use site string as jQuery selector.
var site = (event.target.href.split("#")[1]).replace(/\./g, "\\.");
Now this could be simplified to:
var site = event.target.hash.replace(/\./, "\\."); // get the id
jQuery(site).show();
jQuery('.sitePick').click(function(event) {
var site = event.target.hash.replace(/\./, "\\."); // get the id
jQuery(site).show(); //change css
});
.hidden {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a class="sitePick" href="#site_buzzy.com">Site Pick</a>
<br />
<div id="site_buzzy.com" class="hidden">The DIV #site_buzzy.com</div>
I'm trying to make a add to favorite system. I have a function which alerts the proper id I want to add.
I use:
<script type="text/javascript">
function addfavo(state_name)
{
alert(state_name);
}
</script>
And in my html I have a loop (with php) which shows all the images with the add to favorite links which looks like.
<div style="margin-top:40px;">
<a onclick="addfavo('<?php echo $imgid ?>')"><b>Add to favourits</b></a>
</div>
So what happens is I have a lot of links to the same function with different parameters, but I only want the link that I click to change the text (to something like added to favorites)
Can some one help me in the right direction?
I have tried adding:
$(this).innerHTML("test");
but it didn't work.
You might want to use the html method:
$(this).html('test');
While html is a jQuery method, innerHTML is a property of a DOM element. If you were using pure JavaScript, you'd probably use:
this.innerHTML = 'test';
However, as you are using the onclick attribute on your HTML tag, this will not point to your current DOM element inside your function scope. In your case, I'd add a class to your elements, like add_favorite and add your text to another attribute:
<div style="margin-top:40px;">
<b>Add to favourits</b>
</div>
And then apply a jQuery event to it:
<script type="text/javascript">
$(function() {
$('.add-favorite').click(function(e) {
var text = $(this).data('text'); // store the text in a variable
$(this).html(text); // replace your element's html with your text
e.preventDefault();
});
});
</script>
Fiddle: http://jsfiddle.net/MH6vY/