jQuery, reuse method for many divs [closed] - javascript

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 CoffeeScript code that looks like this:
if $('#user_address_attributes_country').val() == ""
$('#user_address_attributes_country').val("PL")
Now I want to do the same thing for a few divs but without repetitions.
How can it be done by jQuery?

The reason you can't apply it multiple times is because your trying to apply it to an element which has an id. They are unique. Use classes and do this instead:
if ($('.user_address_attributes_country').val("")) {
$('.user_address_attributes_country').val("PL");
}
Simple demo: JsFiddle

Related

Remove div class if display:none [closed]

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
I would like to know if there is a way to remove a class when that class is marked with display: none;. Basically, I would like to prevent it from loading.
Is it possible to do it using JQuery?
Thanks in advance!
$('.class-you-want-to-remove:hidden').removeClass('class-you-want-to-remove');
https://api.jquery.com/hidden-selector/
You can use hide() method in jQuery to hide the blocks under that class.
$(".classname").hide();
Similarly you use show() method to show the blocks under that class.
$(".classname").show();

external json to acces in javascript [closed]

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 5 years ago.
Improve this question
How can I access categories.name_category in Javascript? Everything is working for the AJAX call to the REST API:
I tried to do like this but its not working:
I use Moustache framework
categories is an array of objects, so you need to use {{categories[0].name_category}} - assuming you only want the first value.

How do I access the link to the content? [closed]

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
How do I access the content of link to by JavaScript and jquery
<a href='http://example.com' id='my id'>content</a>
First of all the id cannot contain spaces.
Then with jQuery to access the content the value in the DOM you need to use the .html() after the selector.
Here is a working example :
http://jsfiddle.net/zuz660uk/
If on the other end you want the value of href then use .attr('href')

Using CSS in JS [closed]

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 am trying to change CSS elements depending on situations used in javascript.
an example I had tried was:
if ('image'=="clicked")
{blahblahblah}
'image' would be my div id or div tag in CSS.
I know in Jquery I can use $('#image').click, but I don't think it Jquery will be good for my situation.
Consider document.querySelector. It's powerful, accepting any valid CSS selector. You can even start from another element, such as myElement.querySelector, to only get children of that element that match the selector. Very useful!
Just be aware that IE7 and below do not support this function.
Assuming your id as image
document.getElementById('image').onclick = function() {
alert("button was clicked");
}​;​

jQuery slideUp slideDown help, it's slideToggle [closed]

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
If i click a division it will slidUp and if i click that division again it will slideDown.
How can i do this ?
$("#id_color_tags").click(function(){
$("#rightsidefive").slideToggle();
})
Tried that but that didn't work.
Put an alert in the click event, are you getting to the function?
Are the names right. asp.net can alter names so maybe select on a class name instead. So <div class="thisclass"> and the selector then becomes ".thisclass"
Same applies for the #rightsidediv
Failing that, post some HTML so we can see.

Categories