0

The below function does what it needs to - it checks if the div #searchBar is shown (it's a popup search box), and updates the placeholder attribute of #QuickSearchQuery accordingly.

<input type="search" name="keywords" placeholder="Search..." title="Search" id="QuickSearchQuery" /> 

<script type="text/javascript">
            $(document).ready(function() { 
                if($('#searchBar').css('display') == 'block') {
                    $('#QuickSearchQuery').attr('placeholder','Additional Search Options');
                } else {
                    $('#QuickSearchQuery').attr('placeholder','Search...');
                }
            });
</script>

The problem is that it only does this when the page loads. #searchBar's display can change from block to none based on the user clicking a different button, and when this is done it doesn't change the #QuickSearchQuery attribute.

I can't have the attribute change happen when that button is clicked, unfortunately, because there are other things which can cause #searchBar's display to change (clicking outside of #searchBar's div will set it to 'display: none').

I don't have a live example because the site isn't live yet, but mostly I just want to know if this kind of thing is possible.

Thank you!

3
  • I don't know if it is possible too but, you can use $('selector:visible') to check if an element is visible. Commented Dec 11, 2015 at 16:20
  • Use MutationObservers to watch DOM element attribute changes. Commented Dec 11, 2015 at 16:20
  • Create a custom event and trigger that event to show/hide your item. Then handle your logic in an event handler for the custom event. Commented Dec 11, 2015 at 16:21

5 Answers 5

3

You have written your logic inside document.ready event which is executed only once when page loads. Place your logic inside a function and call it on button click.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, but I can't call it on click because it is based on the #searchBar's display of either 'block' or 'none'. I could bind the attribute changing to the same click that opens #searchBar, but when you click outside of #searchBar it closes the div (changes it to display: none), but it wouldn't change the attribute. Hopefully that makes sense.
I am not sure why this answer was voted up so much. The original question clearly stated placing it in the button click was not an option and explained reasons why. To me this answer only shows lack of reading or understanding the original question.
0

The change of an attribute like this is a DOM Mutation event

You can monitor them using this Mutation events plugin

But to be honest @Akshey Bhat has the simplest solution - channel all your logic through one function if you can.

Comments

0

Your logic written inside document.ready event which execute only when page loads. SO you have also write logic for button click.

Here is some logic that you can use.

<input type="search" name="keywords" placeholder="Search..." title="Search" id="QuickSearchQuery" /> 

<script type="text/javascript">
            $(document).ready(function() { 
                if($('#searchBar').css('display') == 'block') {
                    $('#QuickSearchQuery').attr('placeholder','Additional Search Options');
                } else {
                    $('#QuickSearchQuery').attr('placeholder','Search...');
                }

            });


                $('button').click(function() { 
                    if($('#searchBar').css('display') == 'block') {
                    $('#QuickSearchQuery').attr('placeholder','Additional Search Options');
                } else {
                    $('#QuickSearchQuery').attr('placeholder','Search...');
                }
               });
</script>

1 Comment

The original question stated putting it in button click was not an option and detailed reasons why.
0

The cleanest way to do this is to define a custom event that can be triggered whenever you possibly need to update the behavior of your element. This way, you can trigger the event on document load, and whenever other elements change, and consolidate all your logic in the custom event handler.

Here's a sample where I trigger the myToggle event on document ready and when a toggleSearch button is clicked.

$(document).ready(function() { 
  $("#searchBar").trigger("myToggle");
});

$("#toggleSearch").click(function() {
  $("#searchBar").toggle();
  $("#searchBar").trigger("myToggle");
});

$("#searchBar").on("myToggle", function() {
  if($('#searchBar').is(':visible')) {
    $('#QuickSearchQuery').prop('placeholder','Additional Search Options');
  } else {
    $('#QuickSearchQuery').prop('placeholder','Search...');
  }
});
#searchBar {
  background-color: #CCC;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="searchBar">
  Search Bar
</div>
<input type="search" name="keywords" placeholder="Search..." title="Search" id="QuickSearchQuery" /> 
<input type="button" id="toggleSearch" value="Toggle Advanced Search" />

Comments

0

You can use MutationObservers to watch DOM elements for changes. Below is an example of watching attribute changes. This is for pure javascript. I am not sure if jQuery has anything like this built in.

Fiddle: http://jsfiddle.net/AtheistP3ace/e6c1xdob/

HTML:

<div id="some-id" data-test="somevalue"></div>

JS:

var target = document.querySelector('#some-id');

var observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
        alert(mutation.type);
    });
});

var conf = {
    attributes: true,
    childList: false,
    characterData: false
}

observer.observe(target, conf);

target.setAttribute('data-test', 'newvalue');

Here is an example of watching the searchBar for only style attribute changes. Whenever the style attribute changes lets check if the display changed and update accordingly. This code will work if you are applying display changes inline. If not and are using classes to make these changes you can change this code to watch the class attribute instead to see if a specific class has been applied.

$(document).ready(function() {
    var searchBar = $('#searchBar')[0];
    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            updateAttribute();
        });
    });

    var conf = {
        attributes: true,
        childList: false,
        characterData: false,
        attributeFilter: ['style']
    }

    observer.observe(searchBar, conf);
});

function updateAttribute() {
    if ($('#searchBar').css('display') == 'block') {
        $('#QuickSearchQuery').attr('placeholder', 'Additional Search Options');
    } else {
        $('#QuickSearchQuery').attr('placeholder', 'Search...');
    }
}

Comments

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.