Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
What is more efficient between these two sets, and why. Thanks :)
<button data-target="1" class="target-buttons'>Target</button>
$(document).on('click', '.target-buttons', function() {
alert($(this).data('target'));
});
<button onclick="alertTarget('1')" class="target-buttons">Target</button>
function alertTarget(value) {
alert(value);
}
Thanks very much!
<button onclick="alertTarget('1')" class="target-buttons">Target</button>
function alertTarget(value) {
alert(value);
}
Would be more efficient since it's more re-usable than the former. With this, you don't have to set up a bunch of click events and it's streamlined via html. Technically you do have to set up the click event, but it's more obvious than the former IMO.
Efficiently doesn't particularly matter in this case I don't think.
In general you don't want to mix HTML with JavaScript so the former would be best.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have to get 3 hyperlinks in javascript based only on their href and create an onClick event. In other words, I need to do two things with the below urls:
Get all three a elements based on their URLs, for instance href1,href2,href3
Create an onClick event, for example href1.onClick calls alert("I am from India");
URLs:
<a class="" href="/MyProject/Information/EmpDetails.aspx?userId=79874&countryId=875567" title="">India</a>
<a class="" href="/MyProject/Information/EmpDetails.aspx?userId=98667&countryId=98755" title="">Australia</a>
<a class="" href="/MyProject/Information/EmpDetails.aspx?userId=76456&countryId=87463" title="">New Zealand</a>
Thanks everyone in advance
You can use jquery attribute selector to find the elements, like:
$("a[href$='countryId=875567']").click(function(){
alert("countryId=875567");
});
This will get all hyperlinks that ends with countryId=875567.
If you want the exact href you should use:
$("a[href='http://example.com.br/test']").click(function(){
alert("test");
});
Example: http://jsfiddle.net/v3qpy3gL
Hope it helps.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am looking for the easiest way (coding and execution time) to remove nested tags from a DOM element using javascript.
Example case:
<i><b>This <b><i>is <u>a</u> new</i></b> test</b></i>
Wanted solution:
<i><b>This is <u>a</u> new test</b></i>
The solution needs to work generally with all possible html tags and not just for the above example.
All nested tags need to be eliminated while keeping the inner HTML in the DOM.
The solution may but doesn't need to use jQuery.
Any suggestions?
You could try that for simplicity:
$("body *").each(function () {
if ($(this).parents(this.nodeName.toLowerCase()).length) $(this).contents().unwrap();
});
-DEMO-
Using the find method you could search for all children of the <b> tag inside the <i> tag:
JS:
$('i b').find('b', 'i').contents().unwrap();
This is a minor improvement to A.Woffs solution regarding code execution.
This way one jQuery element less gets initialized per dom node.
But i won't change the accepted answer to this answer for such a minor improvement.
$("body *").each(function () {
var $this = $(this);
if ($this.parents(this.nodeName.toLowerCase()).length) $this.contents().unwrap();
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I use this method
$jq('#indexClassData').on('hidden', function () {
// Do something…
expandIndexClass();
})
It does not work. What am I missing?
You need to wait for the "hidden.bs.collapse" event, not "hidden".
Scroll down to "Events" here: http://getbootstrap.com/javascript/#collapse-usage
thank you but i use another method
// ahmed taha to fix issue of documet coolapse
$jq('#indexClassData').on('shown.bs.collapse', function () {
expandIndexClass()
})
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have 2 separate functionalities on one page of my site, both of which call the same Ajax function. The 2 functionalities need to be distinguishable. My solution was to pass a string (called string here) in one of the functions, such as in the following simplified example.
<script type="text/javascript">
function AjaxFunction(string) {
if (string === 'String') {
alert('You have clicked Button2');
} else {
alert('You have clicked Button1');
}
}
</script>
<button type="button" id="Button1" onclick="AjaxFunction()">Click1</button>
<button type="button" id="Button2" onclick="AjaxFunction('String')">Click2</button>
This seems perfectly fine to me. The only issue is that string is null when Button1 is clicked. I imagine there is no problem with this since the if...else takes care of any ambiguity of what to do with string. I have tested with my actual Ajax function and everything works OK, but since I am still somewhat new to coding I always imagine that stupid mistakes I am not aware of will destroy the site. Sorry for the trivial question but am I missing any performance issues, bugs, etc. with this approach? Thank you for any help!
You need to pass an empty string into the first button call. So:
onclick="AjaxFunction('')"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have <a class="wishlist">TEXT</a>
i need <a class="wishlist" title="TEXT"></a>
I can use only JS.
As your selector is a class, I'd set the function up to handle the possibility of multiples.
$('.wishlist').each(function() {
$(this).attr( 'title', $(this).text() ).empty();
});
Don't forget to either place this code after your HTML, or wrap it in document.ready.
$('.wishlist').attr('title', $('.wishlist').text() );
$(".wishlist").attr("title", $(".wishlist").text()).text("");
That will set the title attribute to the current text, and afterwards unset the text.
try it again:
$('.wishlist').attr('attr','TEXT').html("");
document.getElementsByClassName('wishlist').title = 'TEXT'
document.getElementsByClassName('wishlist').innerHTML = ''